Numpy where function multiple conditions
Categories:
Mastering NumPy's where
Function with Multiple Conditions

Learn how to effectively use NumPy's where
function to apply conditional logic to arrays, handling multiple conditions for powerful data manipulation.
NumPy's where
function is a versatile tool for conditional selection and replacement within arrays. While its basic usage with a single condition is straightforward, applying multiple conditions can sometimes be tricky. This article will guide you through various techniques to combine conditions effectively, enabling you to perform complex data transformations with ease.
Understanding np.where
Basics
Before diving into multiple conditions, let's quickly review the fundamental syntax of np.where
. The function takes three arguments: a condition
, an x
value (returned when the condition is true), and a y
value (returned when the condition is false). These x
and y
values can be scalars or arrays of the same shape as the condition.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
result = np.where(arr > 3, arr * 10, arr)
print(result)
Basic usage of np.where
with a single condition.
Combining Multiple Conditions with Logical Operators
The most common way to handle multiple conditions in np.where
is by using NumPy's element-wise logical operators: &
(AND), |
(OR), and ~
(NOT). These operators work directly on boolean arrays, allowing you to create a single, complex condition from several simpler ones. Remember to enclose each individual condition in parentheses to ensure correct operator precedence.
flowchart TD A[Start] B{Condition 1?} C{Condition 2?} D[Result True] E[Result False] F[End] A --> B B -- True --> C B -- False --> E C -- True --> D C -- False --> E D --> F E --> F
Flowchart illustrating the logic of combining two conditions with an AND operator.
import numpy as np
arr = np.array([10, 20, 30, 40, 50, 60, 70])
# Condition 1: greater than 25
cond1 = arr > 25
# Condition 2: less than 65
cond2 = arr < 65
# Combine with AND (&)
result_and = np.where(cond1 & cond2, arr * 100, arr)
print(f"AND Result: {result_and}")
# Combine with OR (|)
result_or = np.where(cond1 | cond2, arr * 100, arr)
print(f"OR Result: {result_or}")
# Combine with NOT (~)
result_not = np.where(~cond1, arr * -1, arr)
print(f"NOT Result: {result_not}")
Using &
, |
, and ~
for multiple conditions in np.where
.
&
for element-wise logical AND and |
for element-wise logical OR when working with NumPy arrays. Standard Python and
and or
operators will raise an error because they operate on the truthiness of the entire array, not element by element.Nested np.where
for Complex Logic
For more complex scenarios, especially when you need different outcomes for different combinations of conditions, you can nest np.where
calls. This allows you to build a decision tree-like structure. The y
(false) argument of an outer np.where
can itself be another np.where
call, handling subsequent conditions.
import numpy as np
arr = np.array([5, 15, 25, 35, 45, 55])
# If arr < 20, set to 0
# Else if arr < 40, set to 100
# Else, set to 200
result_nested = np.where(arr < 20, 0,
np.where(arr < 40, 100, 200))
print(f"Nested where result: {result_nested}")
Example of nesting np.where
for sequential conditional logic.
np.where
can be powerful, it can also become difficult to read and debug if too many levels are introduced. For very complex logic with many conditions and outcomes, consider using np.select
(discussed next) or a custom function with np.vectorize
.Using np.select
for Multiple Conditions and Choices
When you have multiple conditions and a corresponding choice for each, np.select
is often a cleaner and more efficient solution than deeply nested np.where
calls. It takes two lists: a list of conditions and a list of choices. An optional default
value can be provided for elements that don't satisfy any condition.
import numpy as np
arr = np.array([5, 15, 25, 35, 45, 55, 65])
conditions = [
arr < 20, # Condition 1
(arr >= 20) & (arr < 40), # Condition 2
arr >= 40 # Condition 3
]
choices = [
0, # Choice for Condition 1
100, # Choice for Condition 2
200 # Choice for Condition 3
]
result_select = np.select(conditions, choices, default=-1)
print(f"np.select result: {result_select}")
Applying multiple conditions and choices using np.select
.