Numpy delete row – np.delete(): Remove items/rows/columns from Numpy Array | How to Delete Rows/Columns in a Numpy Array?

Numpy delete row: 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.

How to remove items/rows/columns from Numpy array using np.delete() in Python?

np.remove: Python provides a method to delete elements from the numpy array depending on the index position and its syntax is provided below.

numpy.delete( )

Python’s NumPy library has a method to delete elements. It is done by using the np.delete( ) function.

Syntax:

numpy.delete(arr, obj, axis=None)

Where,

  • arr: Array from which elements are to be deleted
  • obj: Index position or positions from which elements are to be deleted
  • axis: The axis along which we want to delete (1 means delete along columns, 0 means delete along the rows, if None then the array is flattened out and then the elements are to be deleted)

It returns a copy of the array with the required elements deleted.

Delete an Element in 1D Numpy Array by its Index position

np.delete: Let’s see the implementation of it.

Python Program to Delete an element in 1D Numpy Array by its Index position

#Program :

import numpy as np
#Numpy array
arr = np.array([10,20,30,40,50,60,70,80,90])
#deleting a=item at index 2
arr = np.delete(arr, 2)
print('Modified Numpy Array after deleting element at index 2')
print(arr)
Output :
Modified Numpy Array after deleting element at index 2
[10 20 40 50 60 70 80 90]

Delete multiple elements in 1D Numpy Array by its Index position

Let’s see the implementation of it.

Python Program to Delete Multiple Elements in 1D Numpy Array by its Index position

#Program : 

import numpy as np 
#Numpy array 
arr = np.array([10,20,30,40,50,60,70,80,90]) 
#deleting a=item at index 2,5,7 
arr = np.delete(arr, 2) 
print('Modified Numpy Array after deleting element at index 2,5,7') 
print(arr)
Output :
Modified Numpy Array after deleting element at index  2,5,7
[10 20 40 50 70 90]

Deleting rows & columns from a 2D Numpy Array

Delete a column in 2D Numpy Array by its column number

Remove rows from numpy array: 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.

Let’s see the implementation of it.

#program

import numpy as np

arr2d = np.array([[10,20,30],
[40,50,60],
[70,80,90]])
#deleting elements at column 1
arr = np.delete(arr2d, 1, axis=1)
print('Modified Numpy Array by deleting element at index position 2,5,7')
print(arr)
Output :
Modified Numpy Array by deleting element at index position 2,5,7
[[10 30]
[40 60]
[70 90]]

Delete multiple columns in 2D Numpy Array by its column number

To delete multiple columns pass axis=1 and list of column numbers to be deleted along with numpy array to the function.

Let’s see the implementation of it.

#Program :

import numpy as np

arr2D = np.array([[11 ,12, 13, 11],
                [21, 22, 23, 24],
                [31, 32, 33, 34]])
arr2D = np.delete(arr2D, [2,3], axis=1)
print('Modified 2D Numpy Array by removing columns at index 2 and 3')
print(arr2D)
Output :
Modified 2D Numpy Array by removing columns at index 2 and 3
[[11 12]
[21 22]
[31 32]]

Delete a row in 2D Numpy Array by its row number

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.

Let’s see the implementation of it.

Python Program to Delete a row in 2D Numpy Array by its row number

#Program : 

import numpy as np 
arr2D = np.array([[11 ,12, 13, 11], [21, 22, 23, 24], [31, 32, 33, 34]]) 
arr2D = np.delete(arr2D, 0, axis=0) 
print('Modified 2D Numpy Array by removing rowss at index 0') 
print(arr2D)
Output :
Modified 2D Numpy Array by removing rowss at index 0
[[21 22 23 24]
[31 32 33 34]]

Delete multiple rows in 2D Numpy Array by its row number

To delete multiple rows pass axis=0 and list of row numbers to be deleted along with numpy array to np.delete()

#Program :

import numpy as np

arr2D = np.array([[11 ,12, 13, 11],
                [21, 22, 23, 24],
                [31, 32, 33, 34]])
arr2D = np.delete(arr2D, [1, 2], axis=0)
print('Modified 2D Numpy Array by removing rows at index 1 and 2')
print(arr2D)
Output :
Modified 2D Numpy Array by removing rows at index 1 and 2
[[11 12 13 11]]

Delete specific elements in 2D Numpy Array by its index position

When we don’t mention axis value, the default value is none which means the array gets flattened. After that, we use np.delete() to delete elements from rows and columns. The function will return a flattened array without the deleted rows and column elements.

#program :

import numpy as np

arr2D = np.array([[11 ,12, 13, 11],
                [21, 22, 23, 24],
                [31, 32, 33, 34]])
arr2D = np.delete(arr2D, 2)
print('Modified 2D Numpy Array by removing element at row 0 column 2')
print(arr2D)
Output :
Modified 2D Numpy Array by removing element at row 0 column 2
[11 12 11 21 22 23 24 31 32 33 34]

Leave a Comment