Delete elements from a Numpy Array by value or conditions in Python

Deleting elements from a NumPy Array by value or conditions in Python.

In this article we will discuss about how to delete elements from a NumPy Array by based on matching values or multiple conditions.

Remove all occurrences of an element with given value from numpy array :

Suppose we have a NumPy array and we want to delete all the occurrences of that particular element from that Numpy array.

#Program :

import numpy as np
# numpy array created from a list
arr = np.array([40,50,60,70,80,90,40,10,20,40])
print('Numpy Array before deleting all occurrences of 40 :')
print(arr)

# Removing all occurrences of elements with value 40 from numpy array
arr = arr[arr != 40]
print('Modified Numpy Array after deleting all occurrences of 40 :')
print(arr)
Output :
Numpy Array before deleting all occurrences of 40 :
[40 50 60 70 80 90 40 10 20 40]
Modified Numpy Array after deleting all occurrences of 40 :
[50 60 70 80 90 10 20]

The condition arr != 40 returns a bool array of same size as arr with value True at places where value is not equal to 40 and returns False at other places. Like this

[ False True True True True True False True True False]

When we will pass it to [] of numpy array arr then it will select the elemnts whose value is True. Means it will return the elements from arr which are not  equal to 40.

Delete elements in Numpy Array based on multiple conditions :

Like above example, it will create a bool array using multiple conditions on numpy array and when it will be passed to [] operator of numpy array to select the elements then it will return a copy of the numpy array satisfying the condition suppose (arr > 40) & (arr < 80) means elements greater than 40 and less than 80 will be returned.

#Program : 

import numpy as np 
# numpy array created from a list 
arr = np.array([40,50,60,70,80,90,40,10,20,40]) 
print('Numpy Array before deleting any element :') 
print(arr) 

# Remove all occurrences of elements below 40 & greater than 80 
# Means keeping elements greater than 40 and less than 80
arr = arr[ (arr > 40) & (arr < 80) ]
print('Modified Numpy Array after deleting elements :') 
print(arr)
Output :
Numpy Array before deleting any element :
[40 50 60 70 80 90 40 10 20 40]
Modified Numpy Array after deleting elements :
[50 60 70]

Delete elements by value or condition using np.argwhere() & np.delete() :

By using np.argwhere() & np.delete()  we can also delete any elements.

#Program : 

import numpy as np 
# numpy array created from a list 
arr = np.array([40,50,60,70,80,90,40,10,20,40]) 
print('Numpy Array before deleting any element :') 
print(arr) 

# deleting all occurrences of element with value 40
arr = np.delete(arr, np.argwhere(arr == 40))
print('Modified Numpy Array after deleting all occurrences of 40') 
print(arr)
Output : 
Numpy Array before deleting all occurrences of 40 : 
[40 50 60 70 80 90 40 10 20 40] 
Modified Numpy Array after deleting all occurrences of 40 : 
[50 60 70 80 90 10 20]

In the above example np.delete() function will delete the element and np.argwhere() function will detect the index.
Means the condition arr == 40 will return a bool array like

[True False False False False False True False False True]

Where the condition arr == 40 matches it returns True and if condition fails it returns False. And when the bool array will be passed inside the function np.argwhere() then it will return the index positions where the values are True.

i.e

[ [0]
  [6]
  [9]]

These are the indices which represents the value 40.

When this index positions will be passed inside np.delete() function then the element present at those index positions will be deleted.

Delete elements by multiple conditions using np.argwhere() & np.delete() :

The concept is same like above only the difference is the elements will be deleted based on multiple conditions.

So, let’s see the implementation of it.

#Program : 

import numpy as np 
# numpy array created from a list 
arr = np.array([40,50,60,70,80,90,40,10,20,40]) 
print('Numpy Array before deleting any element :') 
print(arr) 

#It will delete all the elements which are greater than 40 and less than 80
arr = np.delete(arr, np.argwhere( (arr >= 40) & (arr <= 80) ))
print('Modified Numpy Array after deleting :') 
print(arr)
Output : 
Numpy Array before deleting any element : 
[40 50 60 70 80 90 40 10 20 40] 
Modified Numpy Array after deleting elements : 
[90 10 20]

Leave a Comment