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.
1 2 
# 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,
1 2 3 4 5 
# 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:
1 2 
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.
1 
[ 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.
1 2 
# 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,
1 2 3 4 5 
# 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:
1 2 
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.
1 
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.
1 2 3 4 5 
# 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:
1 2 
Modified Numpy Array : [ 4 5 7 8 9 10 11 4 5 33 7] 
How did that worked ?
1 
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,
1 
[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.
1 
indexArr = np.argwhere(arr == 6) 
Contents of indexArr are,
1 2 3 
[[ 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.
1 2 3 4 5 
# Delete elements at given index position i.e. elements with value 6 arr = np.delete(arr, indexArr) print('Modified Numpy Array :') print(arr) 
Output:
1 2 
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,
1 
[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.
1 2 3 4 5 
# 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:
1 2 
Modified Numpy Array : [ 4 5 11 4 5 33] 
Complete example is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
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] 
Python Recommendations:
C++ & C++11 Recommendations:

Best C++ Courses for beginners to learn from scratch

5 Top Courses to learn C++11 / C+14

Best books to learn C++11
Leave a Reply