numpy.insert(arr,obj, values, axis=None)
arr : Values are added to the copy of this array. obj : index or indices before which the value will be added. values : Values to be added to arr. Must be of same shape of the arr ( if Axis is present ). If Axis is not specified then matching shape of arr is not prerequisite. axis : (Optional) Direction in which the values to be appended. Arrays are flattened before insert() if axis is not given.
import numpy as np
npr=np.array([5,8,3])
npr1=np.insert(npr,1,10) # adding value ( 10 ) at index position 1
print(npr) # [5 8 3] , No change to original array
print(npr1)# [ 5 10 8 3], element added at 2nd position
Adding value at end of the array by using len().
import numpy as np
npr=np.array([5,8,3])
npr1=np.insert(npr,len(npr),10) # adding at the end
print(npr) # [5 8 3] , No change to original array
print(npr1) # [ 5 8 3 10] , 10 added at the end
This is same as using append() to add value.
import numpy as np
npr=np.array([[0,1,2,4],
[3,4,5,6],
[6,7,8,9]])
#npr1=np.insert(npr,1,[10,11,12,13,14],axis=0) # valueError
npr1=np.insert(npr,1,[10,11,12,13],axis=0)
print(npr1)
Output
[[ 0 1 2 4]
[10 11 12 13]
[ 3 4 5 6]
[ 6 7 8 9]]
Axis are the directions in rows and columns. import numpy as np
npr=np.array([[0,1,2,4],
[3,4,5,6],
[6,7,8,9]])
npr1=np.insert(npr,1,[10,11,12],axis=1)
print(npr1)
Output
[[ 0 10 1 2 4]
[ 3 11 4 5 6]
[ 6 12 7 8 9]]
We can specify multiple index positions to add values.
import numpy as np
npr=np.array([[0,1,2,4],
[3,4,5,6],
[6,7,8,9]])
npr1=np.insert(npr,[1,2,3],[10,11,12],axis=1)
print(npr1)
Output
[[ 0 10 1 11 2 12 4]
[ 3 10 4 11 5 12 6]
[ 6 10 7 11 8 12 9]]
Example
import numpy as np
npr=np.array([[0,1,2,4],
[3,4,5,6],
[6,7,8,9]])
npr1=np.insert(npr,[1,2,3],[99],axis=1)
print(npr1)
Output
[[ 0 99 1 99 2 99 4]
[ 3 99 4 99 5 99 6]
[ 6 99 7 99 8 99 9]]
Using different axis
import numpy as np
npr=np.array([[0,1,2,4],
[3,4,5,6],
[6,7,8,9]])
npr1=np.insert(npr,[1,2,3],[99],axis=0)
print(npr1)
Output
[[ 0 1 2 4]
[99 99 99 99]
[ 3 4 5 6]
[99 99 99 99]
[ 6 7 8 9]
[99 99 99 99]]
If axis is not given then the arrays are flattened before inserting.
import numpy as np
npr=np.array([[0,1,2,4],
[3,4,5,6],
[6,7,8,9]])
npr1=np.insert(npr,1,[10,11,12])
print(npr1)
Output
[ 0 10 11 12 1 2 4 3 4 5 6 6 7 8 9]
By using reshape() we can match the requirements of insert ( to match the shape ) and then use.
import numpy as np
npr=np.array([[0,1,2,4],[3,4,5,6],[6,7,8,9]])
npr1=np.array([10,11,12,13,21,22,23,24,31,32,34,35]) # different shape
#npr1=npr1.reshape(3,4)
npr1=npr1.reshape(npr.shape) # Match the shape of first array
npr2=np.insert(npr,2,npr1,axis=0)
print(npr2)
Output
[[ 0 1 2 4]
[ 3 4 5 6]
[10 11 12 13]
[21 22 23 24]
[31 32 34 35]
[ 6 7 8 9]]
import numpy as np
npr=np.array([[0,1,2,4],[3,4,5,6],[6,7,8,9]])
npr1=np.array([[10],[11],[12]]) # One element for each row
npr2=np.insert(npr,[3,2,1],npr1,axis=1)
print(npr2)
Output
[[ 0 10 1 10 2 10 4]
[ 3 11 4 11 5 11 6]
[ 6 12 7 12 8 12 9]]
Numpy
append() : Adding at the end
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.