In this article, we will discuss how to filter rows of NumPy array by multiple conditions. Before jumping into filtering rows by multiple conditions, let us first see how can we apply filter based on one condition. There are basically two approaches to do so:
Method 1: Using mask array
The mask function filters out the numbers from array arr which are at the indices of false in mask array. The developer can set the mask array as per their requirement–it becomes very helpful when it is tough to form a logic of filtering.
Approach
- Import module
- Make initial array
- Define mask
- Make a new array based on the mask
- Print new array
Program:
Python3
import numpy as np
arr = np.array([x for x in range ( 11 , 20 )])
print ( "Original array" )
print (arr)
mask = [ True , False , True , False , True , True , False , False , False ]
new_arr = arr[mask]
print ( "New array" )
print (new_arr)
|
Output
Original array
[11 12 13 14 15 16 17 18 19]
New array
[11 13 15 16]
Method 2: Using iterative method
Rather than using masks, the developer iterates the array arr and apply condition on each of the array element.
Approach
- Import module
- Create array
- Create an empty array
- Iterate through array
- Select items based on some condition
- Add selected items to the empty array
- Display array
Program:
Python3
import numpy as np
arr = np.array([x for x in range ( 11 , 20 )])
print ( "Original array" )
print (arr)
new_arr = []
for x in arr:
if x % 2 = = 0 :
new_arr.append(x)
new_arr = np.array(new_arr)
print ( "New array" )
print (new_arr)
|
Output
Original array
[11 12 13 14 15 16 17 18 19]
New array
[12 14 16 18]
Now let’s try to apply multiple conditions on the NumPy array
Method 1: Using mask
Approach
- Import module
- Create initial array
- Define mask based on multiple conditions
- Add values to the new array according to the mask
- Display array
Example
Python3
import numpy as np
arr = np.array([x for x in range ( 11 , 40 )])
print ( "Original array" )
print (arr)
mask = (arr > 15 ) & (arr % 2 = = 0 )
new_arr = arr[mask]
print ( "New array" )
print (new_arr)
|
Output
Original array
[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]
New array
[16 18 20 22 24 26 28 30 32 34 36 38]
Method 2: Iterative method
Approach
- Import module
- Create initial array
- Create an empty array
- Iterate through the array
- Select items based on multiple conditions
- Add selected items to the empty list
- Display array
Example
Python3
import numpy as np
arr = np.array([x for x in range ( 11 , 40 )])
print ( "Original array" )
print (arr)
new_arr = []
for x in arr:
if x % 2 = = 0 and x > 15 :
new_arr.append(x)
new_arr = np.array(new_arr)
print ( "New array" )
print (new_arr)
|
Output
Original array
[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]
New array
[16 18 20 22 24 26 28 30 32 34 36 38]
Method 3: Using lambda
Approach
- Import module
- Create initial array
- Apply multiple conditions using lambda function
- Select items accordingly
- Add items to a new array
- Display array
Example
Python3
import numpy as np
arr = np.array([x for x in range ( 11 , 40 )])
print ( "Original array" )
print (arr)
new_arr = list ( filter ( lambda x: x > 15 and x % 2 = = 0 and x % 10 ! = 0 , arr))
new_arr = np.array(new_arr)
print ( "New array" )
print (new_arr)
|
Output
Original array
[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]
New array
[16 18 22 24 26 28 32 34 36 38]