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]
Pandas Tutorials Learn Data Analysis with Python

Pandas Tutorial Part #1  Introduction to Data Analysis with Python

Pandas Tutorial Part #2  Basics of Pandas Series

Pandas Tutorial Part #3  Get & Set Series values

Pandas Tutorial Part #4  Attributes & methods of Pandas Series

Pandas Tutorial Part #5  Add or Remove Pandas Series elements

Pandas Tutorial Part #6  Introduction to DataFrame

Pandas Tutorial Part #7  DataFrame.loc[]  Select Rows / Columns by Indexing

Pandas Tutorial Part #8  DataFrame.iloc[]  Select Rows / Columns by Label Names

Pandas Tutorial Part #9  Filter DataFrame Rows

Pandas Tutorial Part #10  Add/Remove DataFrame Rows & Columns

Pandas Tutorial Part #11  DataFrame attributes & methods

Pandas Tutorial Part #12  Handling Missing Data or NaN values

Pandas Tutorial Part #13  Iterate over Rows & Columns of DataFrame

Pandas Tutorial Part #14  Sorting DataFrame by Rows or Columns

Pandas Tutorial Part #15  Merging or Concatenating DataFrames

Pandas Tutorial Part #16  DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most soughtafter professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.