After fiddling around for a while, I figured things out, and am posting them here hoping it will help others.
Intuitively, np.where is like asking "tell me where in this array, entries satisfy a given condition".
>>> a = np.arange(5,10)
>>> np.where(a < 8) # tell me where in a, entries are < 8
(array([0, 1, 2]),) # answer: entries indexed by 0, 1, 2
It can also be used to get entries in array that satisfy the condition:
>>> a[np.where(a < 8)]
array([5, 6, 7]) # selects from a entries 0, 1, 2
When a is a 2d array, np.where() returns an array of row idx's, and an array of col idx's:
>>> a = np.arange(4,10).reshape(2,3)
array([[4, 5, 6],
[7, 8, 9]])
>>> np.where(a > 8)
(array(1), array(2))
As in the 1d case, we can use np.where() to get entries in the 2d array that satisfy the condition:
>>> a[np.where(a > 8)] # selects from a entries 0, 1, 2
array([9])
Note, when a is 1d, np.where() still returns an array of row idx's and an array of col idx's, but columns are of length 1, so latter is empty array.
NumPy
numpy.org › doc › stable › reference › generated › numpy.where.html
numpy.where — NumPy v2.5 Manual
If all the arrays are 1-D, where is equivalent to: [xv if c else yv for c, xv, yv in zip(condition, x, y)] Examples · Try it in your browser! >>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90]) This can be used on multidimensional arrays too: >>> np.where([[True, False], [True, True]], ...
NumPy
numpy.org › devdocs › reference › generated › numpy.where.html
numpy.where — NumPy v2.6.dev0 Manual
If all the arrays are 1-D, where is equivalent to: [xv if c else yv for c, xv, yv in zip(condition, x, y)] Examples · Try it in your browser! >>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90]) This can be used on multidimensional arrays too: >>> np.where([[True, False], [True, True]], ...
03:35
NumPy np.where() Tutorial - Conditional Selection Made Easy for ...
05:33
Smart Data Filtering with np.where | NumPy Tutorial for Beginners ...
03:10
numpy.where() Function - Explained with Examples - YouTube
08:33
numpy.where() Function - Explained with Examples
09:31
numpy.where() - Explained with examples - YouTube
03:48
Mastering Multiple Conditions in numpy.where function! - YouTube
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.where.html
numpy.where — NumPy v2.2 Manual
If all the arrays are 1-D, where is equivalent to: [xv if c else yv for c, xv, yv in zip(condition, x, y)] Examples · >>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90]) This can be used on multidimensional arrays too: >>> np.where([[True, False], [True, True]], ...
Machine Learning Plus
machinelearningplus.com › blog › how to use numpy where function?
How to Use Numpy Where Function? - machinelearningplus
January 26, 2023 - When the np.where() function is applied to a 2-D numpy array, a tuple containing two arrays is returned. These arrays correspond to respective indices of an element in both dimensions. An element position is determined by taking one value each from arrays in order. See the example below.
DataCamp
datacamp.com › doc › numpy › where
NumPy where()
import numpy as np x = np.array([1, 2, 3, 4, 5]) y = np.array([10, 20, 30, 40, 50]) result = np.where(x > 3, x, y) print(result) In this example, elements from `x` are selected where `x > 3`, and from `y` otherwise, resulting in `array([10, 20, 30, 4, 5])`.
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.where.html
numpy.where — NumPy v2.1 Manual
If all the arrays are 1-D, where is equivalent to: [xv if c else yv for c, xv, yv in zip(condition, x, y)] Examples · >>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90]) This can be used on multidimensional arrays too: >>> np.where([[True, False], [True, True]], ...
Vultr Docs
docs.vultr.com › python › third party › numpy › where()
Python Numpy where() - Conditional Element Search
December 30, 2024 - This snippet extracts the elements from data using the indices found by numpy.where(), displaying the data that meets the condition. Define an array with more complex criteria. Use logical operators like & (and) and | (or) to combine conditions. ... This example searches for values that are greater than 2 and less than 6.
Squash
squash.io › python-numpy-where-tutorial
Python Numpy.where() Tutorial - Squash Labs
August 1, 2023 - The numpy.where() function takes three parameters: condition, x, and y. The condition parameter is a boolean array that specifies the condition for selecting elements. The x parameter is the value to be selected when the condition is True, and the y parameter is the value to be selected when the condition is False. Here is a basic example that demonstrates the usage of numpy.where():
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.where.html
numpy.where — NumPy v2.0 Manual
If all the arrays are 1-D, where is equivalent to: [xv if c else yv for c, xv, yv in zip(condition, x, y)] Examples ·
Python Guides
pythonguides.com › python-numpy-where
Optimizing Data Analysis in Pandas Using np.where() in 2025
May 16, 2025 - In this example, I’ve used np.where() to create a new column called ‘Performance’ that labels each state as either ‘High’ or ‘Regular’ based on their sales figures. Check out Replace Values in NumPy Array by Index in Python
Programiz
programiz.com › python-programming › numpy › methods › where
NumPy where()
The NumPy where() method finds indices that are true in an array based on a given condition. The numpy.where() method returns a new array based on a condition applied to each element of an array.