numpy.insert() – Python | Definition, Syntax, Parameters, Example of Python Numpy.insert() Function

In this tutorial, we will discuss what is python numpy.insert() and how to use numpy.insert()? Also, you can get a good grip on numpy.insert() function in Python by using the example prevailing in this tutorial. Let’s tap on the direct links available here for quick reference on insert an element into NumPy Array in python.

Python numpy.insert()

Python Numpy library provides a function to insert elements in an array. If the insertion is not done in place and the function returns a new array. Moreover, if the axis is not given, the input array is flattened.

Syntax:

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

Parameters:

  • arr: array_like object
    • The array which we give as an input.
  • index: int, slice or sequence of ints
    • The index before which insertion is to be made
  • values: array_like object
    • The array of values to be inserted
  • axis: int, optional
    • The axis along which to insert. If not given, the input array is flattened

Return Values:

  • out: ndarray
    • A copy of arr with given values inserted are given indices.
      • If axis is None, then it returns a flattened array.
      • If axis is 1, then insert column-wise.
      • If axis is 0, then insert row-wise.
    • It doesn’t modify the actual array, rather it returns a copy of the given array with inserted values.

Let’s understand with some of the below-given examples:

numpy.insert() function Example

import numpy as np 
a = np.array([[1,2],[3,4],[5,6]]) 

print 'First array:' 
print a 
print '\n'  

print 'Axis parameter not passed. The input array is flattened before insertion.'
print np.insert(a,3,[11,12]) 
print '\n'  
print 'Axis parameter passed. The values array is broadcast to match input array.'

print 'Broadcast along axis 0:' 
print np.insert(a,1,[11],axis = 0) 
print '\n'  

print 'Broadcast along axis 1:' 
print np.insert(a,1,11,axis = 1)

Output:

First array:
[[1 2]
[3 4]
[5 6]]

Axis parameter not passed. The input array is flattened before insertion.
[ 1 2 3 11 12 4 5 6]

Axis parameter passed. The values array is broadcast to match input array.
Broadcast along axis 0:
[[ 1 2]
[11 11]
[ 3 4]
[ 5 6]]

Broadcast along axis 1:
[[ 1 11 2]
[ 3 11 4]
[ 5 11 6]]

Do Refer: 

Insert an element into a NumPy array at a given index position

Let’s take an array of integers and we want to insert an element 14 at the index position 3. For that, we will call the insert() with an array, index position, and element to be inserted.

import numpy as np
# Create a Numpy Array of integers
arr = np.array([8, 12, 5, 9, 13])
# Insert an element 14 at index position 3
new_arr = np.insert(arr, 3, 14)
print('New Array: ', new_arr)
print('Original Array: ', arr)

Output:

New Array: [ 8 12 5 14 9 13]
Original Array: [ 8 12 5 9 13]

Insert multiple elements into a NumPy array at the given index

In this, we are going to insert multiple elements, for this we pass the elements as a sequence along with the index position.

import numpy as np
# Create a Numpy Array of integers
arr = np.array([8, 12, 5, 9, 13])
# Insert three element at index position 3
new_arr = np.insert(arr, 3, (10, 10, 10))
print('New Array: ', new_arr)

Output:

New Array: [ 8 12 5 10 10 10 9 13]

Insert multiple elements at multiple indices in a NumPy array

In this, we are going to insert multiple elements at multiple indices.

import numpy as np
# Create a Numpy Array of integers
arr = np.array([8, 12, 5, 9, 13])
# Insert three element index position 0, 1 and 2
new_arr = np.insert(arr, (0,1,2), (21, 31, 41))
print('New Array: ', new_arr)

Output:

New Array: [21 8 31 12 41 5 9 13]

So in the above example, you can see that we have added (21,31,41) at (0,1,2) position.

Insert a row into a 2D Numpy array

In this, we are going to insert a row in the array, so we have to pass the axis as 0 and the values as a sequence.

import numpy as np
# Create 2D Numpy array of hard coded numbers
arr = np.array([[2, 3, 4],
                [7, 5, 7],
                [6, 3, 9]])
# Insert a row at index 1
new_arr = np.insert(arr, 1, (4, 4, 4), axis=0)
print(new_arr)

Output:

[[2 3 4]
[4 4 4]
[7 5 7]
[6 3 9]]

Insert a column into a 2D Numpy array

In this, we are going to insert a column in the array, for this we need to pass the axis as 1 and the values as a sequence

import numpy as np
# Create 2D Numpy array of hard coded numbers
arr = np.array([[2, 3, 4],
                [7, 5, 7],
                [6, 3, 9]])
# Insert a column at index 1
new_arr = np.insert(arr, 1, (5, 5, 5), axis=1)
print(new_arr)

Output:

[[2 5 3 4]
[7 5 5 7]
[6 5 3 9]]

So you can see that it inserted a column at index 1.

Here is another way to do the same,

import numpy as np
 # Create 2D Numpy array of hard coded numbers
 arr = np.array([[2, 3, 4],
                 [7, 5, 7], 
                 [6, 3, 9]]) 
# Insert a column at index 1 
new_arr = np.insert(arr, 1,5, axis=1) 
print(new_arr)

Output:

[[2 5 3 4]
[7 5 5 7]
[6 5 3 9]]

Conclusion

In this article, you have seen different uses of numpy.insert(). Thank you!