{"id":6569,"date":"2021-05-21T19:03:42","date_gmt":"2021-05-21T13:33:42","guid":{"rendered":"https:\/\/python-programs.com\/?p=6569"},"modified":"2021-11-22T18:40:47","modified_gmt":"2021-11-22T13:10:47","slug":"numpy-insert-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/numpy-insert-python\/","title":{"rendered":"numpy.insert() \u2013 Python | Definition, Syntax, Parameters, Example of Python Numpy.insert() Function"},"content":{"rendered":"
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.<\/p>\n
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.<\/p>\n
Let’s understand with some of the below-given examples:<\/p>\n Output:<\/strong><\/p>\n Do Refer:\u00a0<\/span><\/p>\n 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.<\/p>\n Output:<\/strong><\/p>\n In this, we are going to insert multiple elements, for this we pass the elements as a sequence along with the index position.<\/p>\n Output:<\/strong><\/p>\n In this, we are going to insert multiple elements at multiple indices.<\/p>\n Output:<\/strong><\/p>\n So in the above example, you can see that we have added (21,31,41) at (0,1,2) position.<\/p>\n 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.<\/p>\nnumpy.insert(arr, index, values, axis=None)<\/code><\/p>\n
<\/a>Parameters:<\/h3>\n
\n
\n
\n
\n
\n
<\/a>Return Values:<\/h3>\n
\n
\n
\n
<\/a>numpy.insert() function Example<\/h2>\n
import numpy as np \r\na = np.array([[1,2],[3,4],[5,6]]) \r\n\r\nprint 'First array:' \r\nprint a \r\nprint '\\n' \r\n\r\nprint 'Axis parameter not passed. The input array is flattened before insertion.'\r\nprint np.insert(a,3,[11,12]) \r\nprint '\\n' \r\nprint 'Axis parameter passed. The values array is broadcast to match input array.'\r\n\r\nprint 'Broadcast along axis 0:' \r\nprint np.insert(a,1,[11],axis = 0) \r\nprint '\\n' \r\n\r\nprint 'Broadcast along axis 1:' \r\nprint np.insert(a,1,11,axis = 1)<\/pre>\n
First array:\r\n[[1 2]\r\n[3 4]\r\n[5 6]]\r\n\r\nAxis parameter not passed. The input array is flattened before insertion.\r\n[ 1 2 3 11 12 4 5 6]\r\n\r\nAxis parameter passed. The values array is broadcast to match input array.\r\nBroadcast along axis 0:\r\n[[ 1 2]\r\n[11 11]\r\n[ 3 4]\r\n[ 5 6]]\r\n\r\nBroadcast along axis 1:\r\n[[ 1 11 2]\r\n[ 3 11 4]\r\n[ 5 11 6]]<\/pre>\n
\n
<\/a>Insert an element into a NumPy array at a given index position<\/h2>\n
import numpy as np\r\n# Create a Numpy Array of integers\r\narr = np.array([8, 12, 5, 9, 13])\r\n# Insert an element 14 at index position 3\r\nnew_arr = np.insert(arr, 3, 14)\r\nprint('New Array: ', new_arr)\r\nprint('Original Array: ', arr)<\/pre>\n
New Array: [ 8 12 5 14 9 13]\r\nOriginal Array: [ 8 12 5 9 13]\r\n\r\n<\/pre>\n
<\/a>Insert multiple elements into a NumPy array at the given index<\/h2>\n
import numpy as np\r\n# Create a Numpy Array of integers\r\narr = np.array([8, 12, 5, 9, 13])\r\n# Insert three element at index position 3\r\nnew_arr = np.insert(arr, 3, (10, 10, 10))\r\nprint('New Array: ', new_arr)<\/pre>\n
New Array: [ 8 12 5 10 10 10 9 13]\r\n\r\n<\/pre>\n
<\/a>Insert multiple elements at multiple indices in a NumPy array<\/h2>\n
import numpy as np\r\n# Create a Numpy Array of integers\r\narr = np.array([8, 12, 5, 9, 13])\r\n# Insert three element index position 0, 1 and 2\r\nnew_arr = np.insert(arr, (0,1,2), (21, 31, 41))\r\nprint('New Array: ', new_arr)<\/pre>\n
New Array: [21 8 31 12 41 5 9 13]\r\n<\/pre>\n
<\/a>Insert a row into a 2D Numpy array<\/h2>\n
import numpy as np\r\n# Create 2D Numpy array of hard coded numbers\r\narr = np.array([[2, 3, 4],\r\n [7, 5, 7],\r\n [6, 3, 9]])\r\n# Insert a row at index 1\r\nnew_arr = np.insert(arr, 1, (4, 4, 4), axis=0)\r\nprint(new_arr)<\/pre>\n