NumPy – Filtering rows by multiple conditions

• Last Updated : 05 Aug, 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:

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
• Make a new array based on the mask
• Print new array

Program:

Python3

 # importing numpy libimport numpy as np # making a numpy arrayarr = np.array([x for x in range(11, 20)]) print("Original array")print(arr) # defining maskmask = [True, False, True, False, True, True, False, False, False] # making new array on conditionsnew_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

 # importing numpy libimport numpy as np # making a numpy arrayarr = np.array([x for x in range(11, 20)]) print("Original array")print(arr) # making a blank listnew_arr = [] for x in arr:  # applying condition: appending even numbers    if x % 2 == 0:        new_arr.append(x) # Converting new list into numpy arraynew_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

Approach

• Import module
• Create initial array
• Define mask based on multiple conditions
• Display array

Example

Python3

 # importing numpy libimport numpy as np # making a numpy arrayarr = np.array([x for x in range(11, 40)]) print("Original array")print(arr) # defining mask based on two conditions:# array element must be greater than 15# and must be a divisible by 2mask = (arr > 15) & (arr % 2 == 0) # making new array on conditionsnew_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

 # importing numpy libimport numpy as np # making a numpy arrayarr = np.array([x for x in range(11, 40)]) print("Original array")print(arr) # making a blank listnew_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 arraynew_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

 # importing numpy libimport numpy as np # making a numpy arrayarr = np.array([x for x in range(11, 40)]) print("Original array")print(arr) # using lambda to apply conditionnew_arr = list(filter(lambda x: x > 15 and x % 2 == 0 and x % 10 != 0, arr)) # Converting new list into numpy arraynew_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]

My Personal Notes arrow_drop_up