How to use NumPy where() with multiple conditions in Python?


The Numpy where() function allows us to perform element-wise conditional operations on array. Numpy is a Python library that is used for numerical computation and data manipulation. To use where() method with multiple conditions in Python we can use logical operators like & (and), | (or) and ~ (not). In this article, we will explore some examples to use numpy where() with multiple method in Python.

Syntax of where() Method

numpy.where(condition, x, y)

Here, the `condition` parameter is a boolean array or a condition that evaluates to a boolean array. The x and y are arrays which are selected based on the condition. If the condition array evaluates to be true then the array x is selected and if the condition is false then y will be chosen. The resulting array will have the same shape as the condition array.

Using Numpy where() Function with Multiple Conditions

We will leverage the logical operators such as & (and), | (or) and ~ (not) when we are using the where() method with multiple conditions in Python. These operators help us in combining multiple conditions to create complex filtering or modification rules for our array.

Example 1: Filtering an Array Based on Multiple Conditions

In the below example, we have an array arr with values from 1 to 9. We define two conditions: condition1 selects even numbers, and condition2 selects numbers greater than 5. By combining these conditions using the logical & operator, we create a composite condition. The where() function applies this condition to the arr array and returns a new array where the selected elements satisfy the conditions, while the rest are replaced with 0.

import numpy as np

# Create an array
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])

# Define conditions
condition1 = arr % 2 == 0  # Select even numbers
condition2 = arr > 5      # Select numbers greater than 5

# Apply conditions using where()
filtered_arr = np.where(condition1 & condition2, arr, 0)

print(filtered_arr)

Output

[0 0 0 0 0 6 0 8 0]

Example 2: Modifying an Array Based on Multiple Conditions

In the below example, we have the same array arr as before. However, instead of replacing the elements that don't satisfy the conditions with 0, we modify them. We use the where() function with the composite condition, and when the condition is met, we multiply the corresponding element by 2; otherwise, we keep the element as it is.

import numpy as np

# Create an array
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])

# Define conditions
condition1 = arr % 2 == 0  # Select even numbers
condition2 = arr > 5      # Select numbers greater than 5

# Modify array elements using where()
modified_arr = np.where(condition1 & condition2, arr * 2, arr)

print(modified_arr)

Output

[ 1  2  3  4  5 12  7 16  9]

Example 3: Modifying a 2D Array Based on Multiple Conditions

In the below example, we have a 2D array arr with dimensions 3x3. We define two conditions using logical operators. The first condition, condition1, selects even numbers, while the second condition, condition2, selects numbers greater than 5. Using the where() function, we apply the composite condition (condition1 & condition2) to the arr array. If the condition is met for an element, we multiply it by 2; otherwise, we keep the element unchanged. As a result, elements 6, 8, and 9 satisfy both conditions and are doubled in the modified array (modified_arr).

import numpy as np

# Create a 2D array
arr = np.array([[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]])

# Define conditions
condition1 = arr % 2 == 0       # Select even numbers
condition2 = arr > 5           # Select numbers greater than 5

# Modify array elements using where()
modified_arr = np.where(condition1 & condition2, arr * 2, arr)

print(modified_arr)

Output

[[ 1  2  3]
 [ 4  5 12]
 [ 7 16  9]]

Conclusion

In this article, we have discussed Numpy where() method and how to use it with multiple conditions in Python. We explored examples of filtering and modifying array based on multiple conditions. We can use where() method with 1D or multidimensional arrays which allows us to apply element-wise conditional operations. Understanding how to combine multiple conditions using logical operators provides the flexibility to construct complex filtering or modification rules. By leveraging NumPy's where() function, you can enhance your data manipulation, analysis, and scientific computing tasks with ease.

Updated on: 13-Oct-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements