NumPy Tutorial NumPy Statistics NumPy References

NumPy - insert() function



The NumPy insert() function inserts values along the given axis before the given indices.

Syntax

numpy.insert(arr, obj, values, axis=None)

Parameters

arr Required. Specify the input array (array_like).
obj Required. Specify object that defines the index or indices before which values is inserted. It can be int, slice or sequence of ints.
values Required. Specify values to insert into arr. If the type of values is different from that of arr, values is converted to the type of arr.
axis Optional. Specify the axis along which to insert values. If axis is None then arr is flattened first.

Return Value

Returns a copy of arr with values inserted.

Example:

In the example below, insert() function is used to insert a scalar value at given index.

import numpy as np

Arr = np.array([[10, 20],
                [30, 40]])

#inserting 5 in the array with axis=None
Arr1 = np.insert(Arr, 1, 5)

#inserting 5 in the array with axis=0
Arr2 = np.insert(Arr, 1, 5, axis=0)

#inserting 5 in the array with axis=1
Arr3 = np.insert(Arr, 1, 5, axis=1)

#displaying results
print("Arr is:")
print(Arr)
print("\nArr1 is (insert with axis=None):")
print(Arr1)
print("\nArr2 is (insert with axis=0):")
print(Arr2)
print("\nArr3 is (insert with axis=1):")
print(Arr3)

The output of the above code will be:

Arr is:
[[10 20]
 [30 40]]

Arr1 is (insert with axis=None):
[10  5 20 30 40]

Arr2 is (insert with axis=0):
[[10 20]
 [ 5  5]
 [30 40]]

Arr3 is (insert with axis=1):
[[10  5 20]
 [30  5 40]]

Example:

In the example below, insert() function is used to insert a list at given index.

import numpy as np

Arr = np.array([[10, 20],
                [30, 40]])

#inserting list with axis=0
Arr1 = np.insert(Arr, 1, [100, 200], axis=0)

#inserting list with axis=1
Arr2 = np.insert(Arr, 1, [100, 200], axis=1)

#displaying results
print("Arr is:")
print(Arr)
print("\nArr1 is (insert with axis=0):")
print(Arr1)
print("\nArr2 is (insert with axis=1):")
print(Arr2)

The output of the above code will be:

Arr is:
[[10 20]
 [30 40]]

Arr1 is (insert with axis=0):
[[ 10  20]
 [100 200]
 [ 30  40]]

Arr2 is (insert with axis=1):
[[ 10 100  20]
 [ 30 200  40]]

Example:

A list can be inserted at multiple indices of an array. Consider the example below:

import numpy as np

Arr = np.array([[10, 20, 30],
                [40, 50, 60],
                [70, 80, 90]])

list1 = [1, 2, 3]
list2 = [[1], [2], [3]]
#inserting list with axis=0
Arr1 = np.insert(Arr, [1, 2], list1, axis=0)

#inserting list with axis=1
Arr2 = np.insert(Arr, [1, 2], list2, axis=1)

#displaying results
print("Arr is:")
print(Arr)
print("\nArr1 is (insert with axis=0):")
print(Arr1)
print("\nArr2 is (insert with axis=1):")
print(Arr2)

The output of the above code will be:

Arr is:
[[10 20 30]
 [40 50 60]
 [70 80 90]]

Arr1 is (insert with axis=0):
[[10 20 30]
 [ 1  2  3]
 [40 50 60]
 [ 1  2  3]
 [70 80 90]]

Arr2 is (insert with axis=1):
[[10  1 20  1 30]
 [40  2 50  2 60]
 [70  3 80  3 90]]

❮ NumPy - Array Manipulation