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

In this article we will discuss different ways to delete elements from a Numpy Array by matching value or based on multiple conditions.

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

Suppose we have a numpy array of numbers i.e.

# Create a numpy array from a list
arr = np.array([4,5,6,7,8,9,10,11,4,5,6,33,6,7])

Now suppose we want to delete all occurrences of 6 from the above numpy array. Let’s see how to do that,

# Remove all occurrences of elements with value 6 from numpy array
arr = arr[arr != 6]

print('Modified Numpy Array by deleting all occurrences of 6')
print(arr)

Output:

Modified Numpy Array by deleting all occurrences of 6
[ 4  5  7  8  9 10 11  4  5 33  7]

How does this worked ?

Basically arr != 6 returned a bool array of same size as arr with True at places where value is not 6 and False at other places i.e.

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

Now if we pass this bool array to [] operator of numpy array arr, then it will select the elements from arr foe which bool array has True at corresponding index. Basically it returns the elements from arr which are not 6. Another point to be noted is that it returns a copy of existing array with elements with value 6. We can assign this new array back to arr to have the deletion effect of all occurrences of 6 from the numpy array.

Delete elements in Numpy Array based on multiple conditions

Suppose we have a numpy array of numbers i.e.

# Create a numpy array from a list
arr = np.array([4, 5, 6, 7, 8, 9, 10, 11, 4, 5, 6, 33, 6, 7])

Now we want to delete all occurrences of elements below 6 & greater than 10 i.e. keep elements between range 6 to 10 only. Let’s see how to do that,

# Remove all occurrences of elements below 6 & greater than 10 i.e. keep elements between range 6 to 10 only
arr = arr[ (arr >= 6) & (arr <= 10) ]

print('Modified Numpy Array by deleting all occurrences of elements not in range 6 to 10 : ')
print(arr)

Output:

Modified Numpy Array by deleting all occurrences of elements not in range 6 to 10 : 
[ 6  7  8  9 10  6  6  7]

We basically created a bool array using multiple conditions on numpy array and then passed that bool array to [] operator of numpy array to select the elements only which satisfies the given conditions. So, it returned a copy of numpy array by selecting values below 6 & greater than 10 only and we assigned this new array back to arr to have the deletion effect.

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

Suppose we have a numpy array of numbers i.e.

arr = np.array([4, 5, 6, 7, 8, 9, 10, 11, 4, 5, 6, 33, 6, 7])

Now let’s delete all occurrences of 6 from the above numpy array using np.argwhere() & np.delete() i.e.

# Single line solution to delete all occurrences of element with value 6
arr = np.delete(arr, np.argwhere(arr == 6))

print('Modified Numpy Array :')
print(arr)

Output:

Modified Numpy Array :
[ 4  5  7  8  9 10 11  4  5 33  7]

How did that worked ?

boolArr = (arr == 6)

arr == 6 Returned a Numpy array of bool type with True at places where arr has 6 and False at other places. Size of this bool array will be equal to size of arr. Therefore contents of boolArr are,

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

Now pass this bool array to np.argwhere() which accepts a bool array and return the index positions where bool array has True value i.e.

indexArr = np.argwhere(arr == 6)

Contents of indexArr are,

[[ 2]
 [10]
 [12]]

These are index positions from array arr where element value is 6. Now pass this index positions to np.delete() to delete elements from arra at given index positions i.e.

# Delete elements at given index position i.e. elements with value 6
arr = np.delete(arr, indexArr)

print('Modified Numpy Array :')
print(arr)

Output:

Modified Numpy Array :
[ 4  5  7  8  9 10 11  4  5 33  7]

It deleted all occurrences of element with value 6.

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

Contents of original Numpy array arr is,

[4, 5, 6, 7, 8, 9, 10, 11, 4, 5, 6, 33, 6, 7]

Let’s delete all occurrences of elements between 6 to 10 in a single line i.e.

# Single line solution to delete all occurrences of element between 6 to 10
arr = np.delete(arr, np.argwhere( (arr >= 6) & (arr <= 10) ))

print('Modified Numpy Array :')
print(arr)

Output:

Modified Numpy Array :
[ 4  5 11  4  5 33]

Complete example is as follows:

import numpy as np

def main():

  # Create a numpy array from a list
  arr = np.array([4,5,6,7,8,9,10,11,4,5,6,33,6,7])

  print('Original Array : ', arr)

  print('*** Delete all occurrences of an element in Numpy Array ***')
  print(arr != 6)
  # Remove all occurrences of elements with value 6 from numpy array
  arr = arr[arr != 6]

  print('Modified Numpy Array by deleting all occurrences of 6')
  print(arr)

  print('*** Delete elements in Numpy Array based on multiple conditions ***')

  # Create a numpy array from a list
  arr = np.array([4, 5, 6, 7, 8, 9, 10, 11, 4, 5, 6, 33, 6, 7])
  print('Original Array : ', arr)

  # Remove all occurrences of elements below 6 & greater than 10 i.e. keep elements between range 6 to 10 only
  arr = arr[ (arr >= 6) & (arr <= 10) ]

  print('Modified Numpy Array by deleting all occurrences of elements not in range 6 to 10 : ')
  print(arr)

  print('*** Delete elements by value using np.argwhere() & np.delete() ***')

  arr = np.array([4, 5, 6, 7, 8, 9, 10, 11, 4, 5, 6, 33, 6, 7])

  print('Original Array : ')
  print(arr)

  boolArr = (arr == 6)
  print('Bool Array with True for elements with value 6 : ')
  print(boolArr)

  indexArr = np.argwhere(boolArr)
  print('Index positions from array arr where element value is 6 :')
  print(indexArr)

  # Delete elements at given index position i.e. elements with value 6
  arr = np.delete(arr, indexArr)
  print('Modified Numpy Array :')
  print(arr)

  arr = np.array([4, 5, 6, 7, 8, 9, 10, 11, 4, 5, 6, 33, 6, 7])

  # Single line solution to delete all occurrences of element with value 6
  arr = np.delete(arr, np.argwhere(arr == 6))
  print('Modified Numpy Array :')
  print(arr)

  arr = np.array([4, 5, 6, 7, 8, 9, 10, 11, 4, 5, 6, 33, 6, 7])

  # Single line solution to delete all occurrences of element between 6 to 10
  arr = np.delete(arr, np.argwhere( (arr >= 6) & (arr <= 10) ))

  print('Modified Numpy Array :')
  print(arr)


if __name__ == '__main__':
  main()

Output:

Original Array :  [ 4  5  6  7  8  9 10 11  4  5  6 33  6  7]
*** Delete all occurrences of an element in Numpy Array ***
[ True  True False  True  True  True  True  True  True  True False  True
 False  True]
Modified Numpy Array by deleting all occurrences of 6
[ 4  5  7  8  9 10 11  4  5 33  7]
*** Delete elements in Numpy Array based on multiple conditions ***
Original Array :  [ 4  5  6  7  8  9 10 11  4  5  6 33  6  7]
Modified Numpy Array by deleting all occurrences of elements not in range 6 to 10 : 
[ 6  7  8  9 10  6  6  7]
*** Delete elements by value using np.argwhere() & np.delete() ***
Original Array : 
[ 4  5  6  7  8  9 10 11  4  5  6 33  6  7]
Bool Array with True for elements with value 6 : 
[False False  True False False False False False False False  True False
  True False]
Index positions from array arr where element value is 6 :
[[ 2]
 [10]
 [12]]
Modified Numpy Array :
[ 4  5  7  8  9 10 11  4  5 33  7]
Modified Numpy Array :
[ 4  5  7  8  9 10 11  4  5 33  7]
Modified Numpy Array :
[ 4  5 11  4  5 33]

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top