NumPy – Select Elements By Condition

In this article we will discuss how to select elements or indices from a Numpy array based on multiple conditions.


Similar to arithmetic operations when we apply any comparison operator to Numpy Array, then it will be applied to each element in the array and a new bool Numpy Array will be created with values True or False.
Suppose we have a Numpy Array i.e.

#Create an Numpy Array containing elements from 5 to 30 but at equal interval of 2
arr = np.arange(5, 30, 2)

It’s contents are,

[ 5  7  9 11 13 15 17 19 21 23 25 27 29]

Let’s select elements from it.

Select elements from a Numpy array based on Single or Multiple Conditions

Let’s apply < operator on above created numpy array i.e.

# Comparison Operator will be applied to all elements in array
boolArr = arr < 10

Comparison Operator will be applied to each element in array and number of elements in returned bool Numpy Array will be same as original Numpy Array. But but for every element that satisfies the condition there will be True in array and False for Others in the returned array.
contents of boolArr are,

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

If we pass this bool Numpy Array to subscript operator [] of original array then it will returns a new Numpy Array containing elements from Original array for which there was True in bool Numpy Array i.e.

# Select elements with True at corresponding value in bool array
newArr = arr[boolArr]

We can do all that in a single line by passing complete comparing expression in [] operator i.e.

newArr = arr[arr < 10]

Just like above, it will return a new Numpy Array with elements < 10 from original Numpy Array i.e.

[5 7 9]

Let’s checkout some other examples,

Select elements from Numpy Array which are divisible by 3 :

Contents of Numpy Array arr,

[ 5  7  9 11 13 15 17 19 21 23 25 27 29]

Now lets’ select elements from this Numpy array which are divisible by 3 i.e.

newArr = arr[arr%3==0]

Contents of Numpy array newArr are,

[ 9 15 21 27]

Select elements from Numpy Array which are greater than 5 and less than 20:

Here we need to check two conditions i.e. element > 5 and element < 20. But python keywords and , or doesn’t works with bool Numpy Arrays. Instead of it we should use & , | operators i.e.

#Select elements from Numpy Array which are greater than 5 and less than 20
newArr = arr[(arr > 5) & (arr < 20)]

arr > 5 returns a bool numpy array and arr < 20 returns an another bool numpy array. Now applying & operator on both the bool Numpy Arrays will generate a new bool array newArr.

Contents of Numpy array newArr are,

[ 7  9 11 13 15 17 19]

Complete example is as follows,

import numpy as np


def main():

   print('Select elements from Numpy Array based on conditions')

   #Create an Numpy Array containing elements from 5 to 30 but at equal interval of 2
   arr = np.arange(5, 30, 2)

   print('Contents of the Numpy Array : ' , arr)

   # Comparision OPerator will be applied to all elements in array
   boolArr = arr < 10

   print('Contents of the Bool Numpy Array : ', boolArr)

   # Select elements with True at corresponding value in bool array
   newArr = arr[boolArr]

   print('Contents of the New Numpy Array : ', newArr)

   newArr = arr[arr < 10]

   print('Contents of the New Numpy Array : ', newArr)

   print('*** Select elements from Numpy Array which are divisible by 3 ***')

   newArr = arr[arr%3==0]

   print('Contents of the Numpy Array : ', newArr)

   print('*** Select elements from Numpy Array which are greater than 5 and less than 20 ***')
   
   newArr = arr[(arr > 5) & (arr < 20)]

   print('Contents of the Numpy Array : ', newArr)

if __name__ == '__main__':
   main()

Output

Select elements from Numpy Array based on conditions
Contents of the Numpy Array :  [ 5  7  9 11 13 15 17 19 21 23 25 27 29]
Contents of the Bool Numpy Array :  [ True  True  True False False False False False False False False False
 False]
Contents of the New Numpy Array :  [5 7 9]
Contents of the New Numpy Array :  [5 7 9]
*** Select elements from Numpy Array which are divisible by 3 ***
Contents of the Numpy Array :  [ 9 15 21 27]
*** Select elements from Numpy Array which are greater than 5 and less than 20 ***
Contents of the Numpy Array :  [ 7  9 11 13 15 17 19]

 

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