{"id":3620,"date":"2021-08-26T07:55:15","date_gmt":"2021-08-26T02:25:15","guid":{"rendered":"https:\/\/python-programs.com\/?p=3620"},"modified":"2021-11-22T18:45:23","modified_gmt":"2021-11-22T13:15:23","slug":"np-delete-remove-items-rows-columns-from-numpy-array","status":"publish","type":"post","link":"https:\/\/python-programs.com\/np-delete-remove-items-rows-columns-from-numpy-array\/","title":{"rendered":"np.delete(): Remove items\/rows\/columns from Numpy Array | How to Delete Rows\/Columns in a Numpy Array?"},"content":{"rendered":"
In this tutorial, we will discuss about how we can remove items\/rows\/columns from the Numpy array. We can remove elements from a NumPy array by index position using numpy.delete( ). Get to know the syntax of numpy.delete() function and learn about all its parameters with a clear explanation. Refer to the further modules to be aware of procedures on how to remove or delete an element or multiple elements in rows\/columns of 1D and 2D Arrays by its Index Position.<\/p>\n
Python provides a method to delete elements from the numpy array depending on the index position and its syntax is provided below.<\/p>\n
numpy.delete( )<\/u><\/strong><\/p>\n Python\u2019s NumPy library has a method to delete elements. It is done by using the numpy.delete(arr, obj, axis=None)<\/em><\/p>\n Where,<\/p>\n It returns a copy of the array with the required elements deleted.<\/p>\n Let’s see the implementation of it.<\/p>\n <\/p>\n Let’s see the implementation of it.<\/p>\n <\/p>\n if we want to delete a column from a 2D numpy array using np.delete() then we have to pass the axis=1 along with numpy array and index of column.<\/p>\n Let’s see the implementation of it.<\/p>\n To delete multiple columns pass axis=1 and list of column numbers to be deleted along with numpy array to the function.<\/p>\n Let’s see the implementation of it.<\/p>\n To delete a row from a 2D numpy array using np.delete() we need to pass the axis=0 along with numpy array and the row index.<\/p>\n Let’s see the implementation of it.<\/p>\n <\/p>\n To delete multiple rows pass axis=0 and list of row numbers to be deleted along with numpy array to np.delete()<\/p>\n When we don\u2019t mention axis value, the default value is none which means the array gets flattened. After that, we use In this tutorial, we will discuss about how we can remove items\/rows\/columns from the Numpy array. We can remove elements from a NumPy array by index position using numpy.delete( ). Get to know the syntax of numpy.delete() function and learn about all its parameters with a clear explanation. Refer to the further modules to be …<\/p>\nnp.delete( )<\/code> function.<\/p>\n
Syntax:<\/pre>\n
\n
<\/a>Delete an Element in 1D Numpy Array by its Index position<\/h3>\n
#Program :\r\n\r\nimport numpy as np\r\n#Numpy array\r\narr = np.array([10,20,30,40,50,60,70,80,90])\r\n#deleting a=item at index 2\r\narr = np.delete(arr, 2)\r\nprint('Modified Numpy Array after deleting element at index 2')\r\nprint(arr)\r\n<\/pre>\n
Output :\r\nModified Numpy Array after deleting element at index 2\r\n[10 20 40 50 60 70 80 90]<\/pre>\n
<\/a>Delete multiple elements in 1D Numpy Array by its Index position<\/h3>\n
#Program : \r\n\r\nimport numpy as np \r\n#Numpy array \r\narr = np.array([10,20,30,40,50,60,70,80,90]) \r\n#deleting a=item at index 2,5,7 \r\narr = np.delete(arr, 2) \r\nprint('Modified Numpy Array after deleting element at index 2,5,7') \r\nprint(arr)<\/pre>\n
Output :\r\nModified Numpy Array after deleting element at index 2,5,7\r\n[10 20 40 50 70 90]<\/pre>\n
<\/a>Deleting rows & columns from a 2D Numpy Array<\/h2>\n
<\/a>Delete a column in 2D Numpy Array by its column number<\/h3>\n
#program\r\n\r\nimport numpy as np\r\n\r\narr2d = np.array([[10,20,30],\r\n[40,50,60],\r\n[70,80,90]])\r\n#deleting elements at column 1\r\narr = np.delete(arr2d, 1, axis=1)\r\nprint('Modified Numpy Array by deleting element at index position 2,5,7')\r\nprint(arr)\r\n<\/pre>\n
Output :\r\nModified Numpy Array by deleting element at index position 2,5,7\r\n[[10 30]\r\n[40 60]\r\n[70 90]]<\/pre>\n
<\/a>Delete multiple columns in 2D Numpy Array by its column number<\/h3>\n
#Program :\r\n\r\nimport numpy as np\r\n\r\narr2D = np.array([[11 ,12, 13, 11],\r\n [21, 22, 23, 24],\r\n [31, 32, 33, 34]])\r\narr2D = np.delete(arr2D, [2,3], axis=1)\r\nprint('Modified 2D Numpy Array by removing columns at index 2 and 3')\r\nprint(arr2D)\r\n<\/pre>\n
Output :\r\nModified 2D Numpy Array by removing columns at index 2 and 3\r\n[[11 12]\r\n[21 22]\r\n[31 32]]<\/pre>\n
<\/a>Delete a row in 2D Numpy Array by its row number<\/h3>\n
#Program : \r\n\r\nimport numpy as np \r\narr2D = np.array([[11 ,12, 13, 11], [21, 22, 23, 24], [31, 32, 33, 34]]) \r\narr2D = np.delete(arr2D, 0, axis=0) \r\nprint('Modified 2D Numpy Array by removing rowss at index 0') \r\nprint(arr2D)<\/pre>\n
Output :\r\nModified 2D Numpy Array by removing rowss at index 0\r\n[[21 22 23 24]\r\n[31 32 33 34]]<\/pre>\n
<\/a>Delete multiple rows in 2D Numpy Array by its row number<\/h3>\n
#Program :\r\n\r\nimport numpy as np\r\n\r\narr2D = np.array([[11 ,12, 13, 11],\r\n [21, 22, 23, 24],\r\n [31, 32, 33, 34]])\r\narr2D = np.delete(arr2D, [1, 2], axis=0)\r\nprint('Modified 2D Numpy Array by removing rows at index 1 and 2')\r\nprint(arr2D)\r\n<\/pre>\n
Output :\r\nModified 2D Numpy Array by removing rows at index 1 and 2\r\n[[11 12 13 11]]<\/pre>\n
<\/a>Delete specific elements in 2D Numpy Array by its index position<\/h3>\n
np.delete()<\/code> to delete elements from rows and columns. The function will return a flattened array without the deleted rows and column elements.<\/p>\n
#program :\r\n\r\nimport numpy as np\r\n\r\narr2D = np.array([[11 ,12, 13, 11],\r\n [21, 22, 23, 24],\r\n [31, 32, 33, 34]])\r\narr2D = np.delete(arr2D, 2)\r\nprint('Modified 2D Numpy Array by removing element at row 0 column 2')\r\nprint(arr2D)\r\n<\/pre>\n
Output :\r\nModified 2D Numpy Array by removing element at row 0 column 2\r\n[11 12 11 21 22 23 24 31 32 33 34]<\/pre>\n","protected":false},"excerpt":{"rendered":"