Python

# NumPy np.where Multiple Conditions

In this tutorial, you will learn the various methods you can use to specify multiple conditions using the np.where() function.

## Method 1 – NumPy.where() Multiple Conditions Using the AND Operator

The where() function in NumPy allows us to select elements from a given array that match a given condition. By default, the function accepts a single condition to match against the given array of elements. What if we need to check for multiple conditions in a given array? For that purpose, we can use Python’s logical operators to accomplish that. One such operator is the & (AND) operator. It allows us to specify multiple conditions inside the where function and add and join them with the & operator. The function will take all the specified conditions and return the elements that match all the conditions.

We can illustrate this with an example as shown below:

import numpy as np
arr = np.array([100,120,344,232,343,456,674,637])
new_arr = arr[np.where((arr>300) & (arr<500))]
print(new_arr)

In the program above, we start by creating an array holding a set of integers. We then create a new array and use the where the function to filter for multiple conditions. If any element in the array is greater than 300 and less than 500, add it to the new array. Finally, we print the resulting array:

Using the & operator ensures that both of the conditions are true.

## Method 2 – NumPy.where() Multiple Conditions Using the OR Operator

In some cases, you may want only one of the multiple conditions to be true. For that, we can use the OR operator. This tells the where the function to evaluate all the conditions provided and if any element in the given array matches one of them, add it to the result.

Take the example result shown below:

import numpy as np
arr = np.array([100,120,344,232,343,456,674,637])
new_arr = arr[np.where((arr>300) | (arr==500))]
print(new_arr)

In the example above, we use the | operator to specify multiple conditions and store the resulting values in the new_arr variable. If any element of the input array is greater than or equal to 500, add it to the new array.

The resulting output:

We can see that all the returned elements are greater than 300.

## Method 3 – Numpy.where() Multiple Conditions With Numpy.logical_and() Function

If you do not wish to use Python’s logical operators, NumPy has a logical_and() function that can replace the & operator.

The function is used to determine the element-wise truth of a value of an AND gate. Let us see how we can adopt this function to specify multiple conditions in a NumPy where() function.

import numpy as np
arr = np.array([100,120,344,232,343,456,674,637])
new_arr = arr[np.where(np.logical_and(arr>300, arr<400))]
print(new_arr)

In the code above, we start by defining an array of integers whose elements we wish to filter.

We then use the np.where() function and pass the conditions we wish to evaluate. In this case, we pass them as arguments of the np.logical_and function.

The function evaluates the conditions and filters the elements that match the specified values.

An example output is as shown:

From the output, only two elements match the specified conditions.

## Method 4 – Numpy.where() Multiple Conditions With Numpy.logical_or() Function

Similarly, NumPy has a function that performs the same task as the Python’s logical OR. The numpy.logical_or() function is used to calculate the element-wise true value of an OR gate.

For our case, we can specify multiple conditions and pass them to the np.where() function.

An example code is as shown below:

import numpy as np
arr = np.array([100,120,344,232,343,456,674,637])
new_arr = arr[np.where(np.logical_or(arr>300, arr%2==0))]
print(new_arr)

Here, the function should return all the elements that are either greater than 300 or ones that even number.

The resulting values are:

It works.

## Closing

That’s it for this one. In this tutorial, you learned how to specify multiple conditions in the NumPy where() function using the logical or, logical and, numpy’s logical_and function, and numpy’s logical_or function.