Open In App

numpy.where() in Python

Last Updated : 30 Sep, 2025
Comments
Improve
Suggest changes
22 Likes
Like
Report

numpy.where() is used for conditional selection and replacement in NumPy arrays. It can be used to:

  • Find indices that satisfy a condition
  • Build a new array by choosing values from two options depending on a condition.

In this example, np.where() is used with only a condition to get the indices where elements are greater than 20.

Python
import numpy as np
arr = np.array([10, 15, 20, 25, 30])
result = np.where(arr > 20)
print(result)

Output
(array([3, 4]),)

Explanation:

  • arr > 20 produces a boolean mask: [False, False, False, True, True].
  • np.where(...) with a single argument returns a tuple containing the indices of True entries.
  • The returned value (array([3, 4]),) indicates elements at positions 3 and 4 satisfy the condition.

Syntax:

numpy.where(condition[, x, y])

Parameters:

  • condition: boolean array (or expression producing one).
  • x (optional): array-like or scalar used where condition is True.
  • y (optional): array-like or scalar used where condition is False.

Return value:

  • With only condition: returns a tuple of index arrays (one per dimension).
  • With x and y: returns a new array choosing from x where True, y where False (supports broadcasting and dtype rules).

Uses of numpy.where()

1. Find indices that satisfy a condition

In this example, numpy.where() checks where the condition arr % 2 == 0 is true and returns the indices.

Python
import numpy as np
arr = np.array([2, 7, 8, 3, 10])
idx = np.where(arr % 2 == 0)   
print(idx)
print("Even elements:", arr[idx])

Output
(array([0, 2, 4]),)
Even elements: [ 2  8 10]

Explanation:

  • arr % 2 == 0 creates a boolean mask for even numbers.
  • np.where(...) returns indices of True values: (array([0,2,4]),).
  • arr[idx] uses those indices to extract the even elements.

2. Binary replacement with scalars (x and y)

By providing x and y as arguments, you can use numpy.where() to return different values depending on whether condition is true or false.

Here, numpy.where() function checks the condition arr > 20.

Python
import numpy as np
arr = np.array([10, 15, 20, 25, 30])
result = np.where(arr > 20, 1, 0)
print(result)

Output
[0 0 0 1 1]

Explanation:

  • arr > 20 builds the boolean mask.
  • np.where(mask, 1, 0) returns an array with 1 where mask is True, else 0.
  • Result is an integer array representing the binary condition.

3. Pick from two arrays elementwise

In this example, for elements where condition arr1 > 20 is true, corresponding element from arr1 is chosen. Otherwise, element from arr2 is selected.

Python
import numpy as np
arr1 = np.array([10, 15, 20, 25, 30])
arr2 = np.array([100, 150, 200, 250, 300])
result = np.where(arr1 > 20, arr1, arr2)
print(result)

Output
[100 150 200  25  30]

Explanation:

  • arr1 > 20 identifies where to take values from arr1 (True for indices 3 and 4).
  • np.where(condition, arr1, arr2) returns values from arr1 where True, otherwise from arr2.
  • Final array picks arr2 values where condition is False (indices 0–2) and arr1 values where condition is True (indices 3–4).

4. Replace negatives with zero

This example replaces negative numbers in a 2-D array with 0.

Python
import numpy as np
mat = np.array([[ 5, -2,  3], [-1,  4, -6]])
result = np.where(mat < 0, 0, mat)
print(result)

Output
[[5 0 3]
 [0 4 0]]

Explanation:

  • mat < 0 makes a boolean matrix of the same shape.
  • np.where(..., 0, mat) returns a new array where negatives are replaced by 0, others kept.
  • The function broadcasts scalars (0) across the array shape.

Explore