NumPy where() Multiple Conditions

Spread the love

Python NumPy where() is used to get an array with selected elements from the existing array by checking single or multiple conditions. It returns the indices of the array for with each condition being True. Using & Operator, | Operator, NumPy.logical_and() and, numpy.logical_or() functions along with where() function based on multiple conditions.

In this article, I will explain how to use multiple conditions using & Operator, | Operator, numpy.logical_and() and, numpy.logical_or() function with examples.

1. Quick Examples of Using where() with Multiple Conditions

If you are in a hurry, below are some quick examples of how to use multiple conditions in where condition.


# Below are the quick examples

# Example 1:  Get the indices of array elements
arr1=np.where(arr)

# Example 2: Use numpy.where() multiple conditions with the & operator
arr2 = arr[np.where((arr >5) & (arr 5) | (arr % 5 == 0))]

# Example 4: Find number of values meet in a condition
arr2 = (arr[np.where((arr >5) | (arr % 5 == 0))]).size

# Example 5: Use numpy.where() multiple conditions
# with the numpy.logical_and()
arr2 = arr[np.where(np.logical_and(arr >5, arr 5, arr % 5 == 0))] 

2. Usage of NumPy where() Multiple Conditions

First, let’s quickly see how to use a where() function, and later I will cover examples of how to use it with multiple conditions. When we pass the NumPy array to the numpy.where() it will return the indices of NumPy array elements. Let’s create NumPy array using numpy.array().


# Create a numpy array 
arr = np.array([5,10,15,20,25])
# Get the indices of array elements
arr1=np.where(arr)
print(arr1)

# Output :
(array([0, 1, 2, 3, 4], dtype=int64),)

3. NumPy where() Multiple Conditions With the & Operator

To select the NumPy array elements from the existing array-based on multiple conditions using & operator along with where() function. You can specify multiple conditions inside the where() function by enclosing each condition inside a pair of parenthesis and using an & operator. Let’ pass the multiple conditions with the help of & operator to where() function. For example,


# Use numpy.where() multiple conditions with the & operator
arr2 = arr[np.where((arr >5) & (arr <25))]
print(arr2)

# Output
# [10 15 20]

4. where() Multiple Conditions With the | Operator

You can also use the | operator to specify multiple conditions inside the numpy.where() function. You can specify multiple conditions inside this function by enclosing each condition inside a pair of parenthesis and using an | operator. For example,


# Use numpy.where() multiple conditions with the | operator
arr2 = arr[np.where((arr >5) | (arr % 5 == 0))]
print(arr2)

# Output
# [ 5 10 15 20 25]

Notice that from the above, five values have been returned.

You can also use the size function to simply find how many values meet one of the conditions using (arr[np.where()]).size function.


# Find number of values meet in a condition
arr2 = (arr[np.where((arr >5) | (arr % 5 == 0))]).size
print(arr2)

# Output
# 5

5. Use where() Multiple Conditions With the logical_and()

numpy.logical_and() the function is used to calculate the element-wise truth value of AND gate in Python. Using this function inside the where() function to specify multiple conditions and get the selected elements from an existing array.


# Use numpy.where() multiple conditions with the numpy.logical_and()
arr2 = arr[np.where(np.logical_and(arr >5, arr <25))]
print(arr2)

# Output
# [10 15 20]

6. Use where() Multiple Conditions With the logical_or()

The numpy.logical_or() function is used to calculate the element-wise truth value of OR gate in Python. You can use this function inside the where() function to specify multiple conditions.


# Use numpy.where() multiple conditions with the .logical_or()
arr2 = arr[np.where(np.logical_or(arr >5, arr % 5 == 0))]
print(arr2)

# Output
# [ 5 10 15 20 25]

7. Conclusion

In this article, I have explained how to use multiple conditions on NumPy where() function by using the &(and), |(or), logical_and() and, logical_or() functions

Happy Learning!!

References

Leave a Reply

You are currently viewing NumPy where() Multiple Conditions