Skip to content
Related Articles

Related Articles

NumPy – Filtering rows by multiple conditions
  • Last Updated : 25 Feb, 2021

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 its 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




# importing numpy lib
import numpy as np
  
# making a numpy array
arr = np.array([x for x in range(11, 20)])
  
print("Orignial array")
print(arr)
  
# defining mask
mask = [True, False, True, False, True, True, False, False, False]
  
# making new array on conditons
new_arr = arr[mask]
  
print("New array")
print(new_arr)

Output

Orignial 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




# importing numpy lib
import numpy as np
  
# making a numpy array
arr = np.array([x for x in range(11, 20)])
  
print("Orignial array")
print(arr)
  
# making a blank list
new_arr = []
  
for x in arr:
  # applying condition: appending even numbers
    if x % 2 == 0:
        new_arr.append(x)
  
# Converting new list into numpy array
new_arr = np.array(new_arr)
print("New array")
print(new_arr)

Output

Orignial 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




# importing numpy lib
import numpy as np
  
# making a numpy array
arr = np.array([x for x in range(11, 40)])
  
print("Orignial array")
print(arr)
  
# defining mask based on two conditions: 
# array element must be greater than 15 
# and must be a divisible by 2
mask = (arr > 15) & (arr % 2 == 0)
  
# making new array on conditons
new_arr = arr[mask]
print("New array")
print(new_arr)

Output

Orignial 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




# importing numpy lib
import numpy as np
  
# making a numpy array
arr = np.array([x for x in range(11, 40)])
  
print("Orignial array")
print(arr)
  
# making a blank list
new_arr = []
  
for x in arr:
    # applying two conditions: number is divisible by 2 and is greater than 15
    if x % 2 == 0 and x > 15:
        new_arr.append(x)
  
# Converting new list into numpy array
new_arr = np.array(new_arr)
print("New array")
print(new_arr)

Output

Orignial 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




# importing numpy lib
import numpy as np
  
# making a numpy array
arr = np.array([x for x in range(11, 40)])
  
print("Orignial array")
print(arr)
  
# using lambda to apply condition
new_arr = list(filter(lambda x: x > 15 and x % 2 == 0 and x % 10 != 0, arr))
  
# Converting new list into numpy array
new_arr = np.array(new_arr)
print("New array")
print(new_arr)

Output

Orignial 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]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :