AppDividend
Latest Code Tutorials

Python Numpy insert() Function | Numpy.insert()

0

Python numpy insert() is an inbuilt numpy method that is used to insert a given value in a ndarray before a given index along with the given axis. Means, the value will be inserted before the value present in the given index in a given array. If the axis is not mentioned, then an input array is flattened.

Python Numpy insert()

Numpy insert() function inserts values in the input array along the given axis and before a given index. If a type of values is converted to be inserted, it is different from an input array. Insertion is not done in place, and the method returns the new array. Also, if the axis is not mentioned, an input array is flattened.

Syntax

numpy.insert(array, object, values, axis = None)

Parameter(s)

The numpy.insert() function takes four parameters, those are –

  1. array -> is the name of the array in which the value to be inserted
  2. object -> This can be an integer or a list of an array (subarray) before which the given value is to be inserted.
  3. values -> This is the value that is to be inserted in the array. If the type of value is not the same as the type of the array, then the value is converted into that type.
  4. axis -> This is applicable for multidimensional array. By default, the value of the axis is None. But, for the multidimensional array, it helps us to insert the value in a particular axis.

Return Value

The insert() method returns a copy of the array with the value which is inserted as per the mentioned object in a particular axis.

Python Numpy: Insert in a 1D array

See the following code.

# Importing numpy
import numpy as np

# We will create an 1D array

# this will create an array with values 10 to 15
arr1 = np.arange(10, 16)
# Printing the array
print("The array is: ", arr1)
# Printing shape of the array
print("Shape of the array is : ", np.shape(arr1))

# Now we will insert a value before the value 12
# See, 4 is present at index 5
obj = 2
value = 40

# Inserting value
arr = np.insert(arr1, obj, value, axis=None)

# Printing new array
print("After inserting the new array is: ")
print(arr)
print("Shape of the new array is : ", np.shape(arr))

Output

The array is:  [10 11 12 13 14 15]
Shape of the array is :  (6,)
After inserting the new array is: 
[10 11 40 12 13 14 15]
Shape of the new array is :  (7,)

Explanation

In this example, we have first created an array of size 6 with values 10-15. Now, we want to insert the value 40, before the value 12. So, we can see that the position of 12 is at index=2, and we want to insert value at index =2. As a result, the index of 12 will be shifted to index 3, and later values will also be shifted.

We can see in the output that the value 40 is inserted before at index 2, as we gave the value of object =2 and value=40.

Python numpy: Insert in Multidimensional Array

See the following code.

# Importing numpy
import numpy as np

# We will create a 2D array
# Of shape 4x3
arr1 = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9), (50, 51, 52)])
# Printing the array
print("The array is: ")
print(arr1)
print("The shape of the array is: ", np.shape(arr1))

# Now we will insert values before one particular axis

# this will insert two new rows with value
# 50 and 100 respectively before the row 1
a = np.insert(arr1, 1, [[50], [100], ], axis=0)

# printing new array
print("New array is : ")
print(a)
print("Shape of the array is: ", np.shape(a))

Output

The array is: 
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [50 51 52]]
The shape of the array is:  (4, 3)
New array is : 
[[  1   2   3]
 [ 50  50  50]
 [100 100 100]
 [  4   5   6]
 [  7   8   9]
 [ 50  51  52]]
Shape of the array is:  (6, 3)

Explanation

In this example, we can see that we have first initialized an array of size 4×3. Then we wanted to insert values before the second row (index=1).

So, we called insert() and passed the value of object 1, and we wanted to insert two rows with values 50 and 100, so we passed value as a list of [50,100], and axis=0 means row-wise.

Conclusion

Python numpy insert() method inserts a given value in a ndarray before a given index along with the given axis. If you don’t specify the axis, then it flattens the ndarray.

See also

Python numpy arange()

Python numpy append()

Python numpy delete()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.