The insert()
function in Python is used to insert values along the given axis of an array before the given index position.
The insert()
function has the following syntax:
numpy.insert(arr, obj, values, axis=None)
The insert()
function takes the following parameters:
arr
: This is the input array. It is a required parameter.obj
: This is the object that specifies the index position before which the values are inserted into the input array. It is a required parameter.values
: This represents the values to be inserted into the input array. It is a required parameter.axis
: This is the axis along which the values are inserted. It is an optional parameter.The insert()
function returns a copy of the input array but with new values inserted into it.
import numpy as np# creating an input arraya = np.arange(9).reshape(3,3)# calling the insert() functionmy_array = np.insert(a, 1, [10, 11, 12], axis = 0 )# printing the old arrayprint("This is th input array", a)# printing the new arrayprint("This is the new array", my_array)
numpy
module.a
.insert()
function on the array, a
, and assign the result to a variable, my_array
.a
.my_array
.