How to Use numpy.insert() Function?

Learn via video courses
Topics Covered

We can perform complex and computationally costly calculations because of Python's very robust NumPy library. We can use arrays and operate with them in a variety of ways using different functions. The NumPy insert() method is one such function.

The NumPy insert() function inserts a value/values along the specified axis at a given index. For example, let's assume we have an array arr [1, 2, 3, 4, 5]. If we want to insert 10 at the 3rd index, we would simply use NumPy insert() as insert(arr, 3, 10). Hence, the output would be [ 1 2 3 10 4 5].

Syntax for NumPy insert()

The syntax for NumPy insert() is :

Parameters for NumPy insert()

The parameters that NumPy insert() takes in are :

  • arr :
    This mandatory parameter represents the input array in which an element is to be inserted.
  • index :
    This mandatory parameter represents the position where the element will be inserted in the input array.
  • values :
    This mandatory parameter represents the specific values that are to be inserted into the input array.
  • axis :
    This optional parameter represents the axis along which elements are to be inserted in an array. If the axis is None, the values are inserted at the specified position. If the axis is set to 0, the elements are inserted row-wise. Else if the axis is 1, elements are inserted column-wise.

Return Type for NumPy insert()

NumPy insert() doesn't modify the original array. Hence, it returns a copy of the original collection, with the values positioned according to the parameters.

Examples for NumPy insert()

  • Insert an element into a NumPy array at a given index position:
    Using NumPy insert(), we can insert an element in a list at any position that we want. In this example, we will insert 10 in an array consisting of integers from 1 to 8 at the third position.

    Output:

  • How to use numpy.insert() with Scalars:
    Using NumPy insert(), we can insert integer values as scalars in our original array at specified indexes.

    Output :

  • Insert multiple elements at multiple indices in a NumPy array :
    We can insert multiple elements at different indices using the NumPy insert() function. To further discuss this topic, we will insert the following:

    • 21 at index 1.
    • 34 at index 4.

    Output:

  • Insert a row into a 2D Numpy array:
    We can insert a whole row into an array using the NumPy insert() function by providing the index of the row with the respective row elements. To perform this, we need to specify the row index and set the axis as 0.

    Output:

  • Insert a column into a 2D Numpy array:
    Just as we inserted a row into a two-dimensional NumPy array, we can insert a column into a two-dimensional NumPy array too. To achieve this, we must specify the column index and set the axis as 1.

    Output:

Conclusion

  • In this article, we understood what NumPy insert() is, a function that allows us to insert elements/elements at a specific position of a NumPy array.
  • We covered the syntax of NumPy insert(), and the various parameters that allow us to insert single elements or rows and columns into one and two-dimensional arrays.
  • We can insert rows and columns using NumPy insert() too. All we need to do is specify the optional parameter axis as 0 for rows and 1 for columns.