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.

Answer from Alex Dalyac on Stack Overflow
🌐
NumPy
numpy.org › doc › 2.5 › reference › generated › numpy.where.html
numpy.where — NumPy v2.5 Manual
numpy.where(condition, [x, y, ]/)# Return elements chosen from x or y depending on condition. Note · When only condition is provided, this function is a shorthand for np.asarray(condition).nonzero(). Using nonzero directly should be preferred, as it behaves correctly for subclasses.
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › numpy-where-in-python
numpy.where() in Python - GeeksforGeeks
September 30, 2025 - With x and y: returns a new array choosing from x where True, y where False (supports broadcasting and dtype rules). In this example, numpy.where() checks where the condition arr % 2 == 0 is true and returns the indices.
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.argwhere.html
numpy.argwhere — NumPy v2.3 Manual
where, nonzero · Notes · np.argwhere(a) is almost the same as np.transpose(np.nonzero(a)), but produces a result of the correct shape for a 0D array. The output of argwhere is not suitable for indexing arrays. For this purpose use nonzero(a) instead. Examples · Try it in your browser! >>> import numpy as np >>> x = np.arange(6).reshape(2,3) >>> x array([[0, 1, 2], [3, 4, 5]]) >>> np.argwhere(x>1) array([[0, 2], [1, 0], [1, 1], [1, 2]]) Go BackOpen In Tab ·
🌐
DataCamp
datacamp.com › doc › numpy › where
NumPy where()
The `where()` function in NumPy is used for array computation and analysis; it returns elements chosen from `x` or `y` depending on `condition`, or the indices of elements that meet the condition when only `condition` is provided.
🌐
Medium
medium.com › @heyamit10 › understanding-numpy-where-with-two-conditions-21871ed01aa6
Understanding numpy.where() with Two Conditions | by Hey Amit | Medium
March 6, 2025 - Think of numpy.where() as a decision-maker for your arrays. It checks a condition, and based on whether it’s True or False, it picks a value for you.
🌐
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 3-D numpy array, a tuple containing three arrays is returned. These arrays correspond to respective indices of an element in all three dimensions.
Find elsewhere
🌐
Programiz
programiz.com › python-programming › numpy › methods › where
NumPy where() | Programiz
August 30, 2023 - 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.
🌐
SciPy
docs.scipy.org › doc › numpy-1.8.1 › reference › generated › numpy.where.html
numpy.where — NumPy v1.8 Manual
numpy.where(condition[, x, y])¶ · Return elements, either from x or y, depending on condition. If only condition is given, return condition.nonzero(). See also · nonzero, choose · Notes · If x and y are given and input arrays are 1-D, where is equivalent to: [xv if c else yv for (c,xv,yv) ...
🌐
IONOS
ionos.com › digital guide › websites › web development › python np.where method
What is np.where in Python? - IONOS
February 1, 2025 - The Python function np.where() is a powerful method from the NumPy library and is used for selecting elements from an array.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.argwhere.html
numpy.argwhere — NumPy v2.6.dev0 Manual
where, nonzero · Notes · np.argwhere(a) is almost the same as np.transpose(np.nonzero(a)), but produces a result of the correct shape for a 0D array. The output of argwhere is not suitable for indexing arrays. For this purpose use nonzero(a) instead. Examples · Try it in your browser! >>> import numpy as np >>> x = np.arange(6).reshape(2,3) >>> x array([[0, 1, 2], [3, 4, 5]]) >>> np.argwhere(x>1) array([[0, 2], [1, 0], [1, 1], [1, 2]]) Go BackOpen In Tab ·
🌐
Reddit
reddit.com › r/learnpython › how does np.where() work?
r/learnpython on Reddit: How does np.where() work?
March 31, 2022 -

My understanding of np.where() was so far as sort of an arg container for .loc. However in the example below, .loc works, but not when np.where is used as arg. Why is that happening?

df=pd.DataFrame({'Name':['Tom', 'Mia', 'Sam'], 'Age':[15, 26, 32]}, index=['A','B','C'])

print(df.loc[(df['Age']>28)&(df['Name'].str.startswith('S')])

k=np.where((df['Age']>28)&(df['Name'].str.startswith('S'))

print(df.loc[k])

🌐
NumPy
numpy.org › doc › 1.15 › reference › generated › numpy.where.html
numpy.where — NumPy v1.15 Manual
numpy.where(condition[, x, y])¶ · Return elements, either from x or y, depending on condition. If only condition is given, return condition.nonzero(). See also · nonzero, choose · Notes · If x and y are given and input arrays are 1-D, where is equivalent to: [xv if c else yv for (c,xv,yv) ...
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.argwhere.html
numpy.argwhere — NumPy v2.1 Manual
where, nonzero · Notes · np.argwhere(a) is almost the same as np.transpose(np.nonzero(a)), but produces a result of the correct shape for a 0D array. The output of argwhere is not suitable for indexing arrays. For this purpose use nonzero(a) instead. Examples · >>> import numpy as np >>> x = np.arange(6).reshape(2,3) >>> x array([[0, 1, 2], [3, 4, 5]]) >>> np.argwhere(x>1) array([[0, 2], [1, 0], [1, 1], [1, 2]]) On this page
🌐
Medium
medium.com › @whyamit404 › what-is-numpy-where-and-how-does-it-work-acd87afb1141
What is numpy.where and How Does it Work? | by whyamit404 | Medium
February 26, 2025 - In Python, the numpy.where function is your tool for making similar decisions within arrays.
🌐
Medium
medium.com › @chanushkr › understanding-numpy-where-vs-pandas-dataframe-where-a-comprehensive-guide-457b5c0403c1
🧩 Understanding numpy.where vs. pandas.DataFrame.where: A Comprehensive Guide 🚀 | by Chanush KR | Medium
July 26, 2024 - 🎯 numpy.where: The “If-Else” of Arrays numpy.where is a versatile function from the NumPy library used for conditional selection and replacement in arrays.
🌐
StrataScratch
stratascratch.com › blog › exploring-numpy-where-in-python
Exploring NumPy where() in Python for Conditional Operations - StrataScratch
September 17, 2025 - In this case, chaining and combining conditions with np.where() would be a good choice. Logical operators like & (and), | (or), and ~ (not) with parentheses can let you make your own logic. We'll use scores to expand on our previous example. Mark a score as "Medium" if it falls between 50 and 80. ... Let’s see the code. import numpy as np scores = np.array([[45, 82, 60], [30, 55, 90]]) labels = np.where(scores >= 80, 'High', np.where((scores >= 50) & (scores < 80), 'Medium', 'Low')) print(labels)
🌐
NumPy
numpy.org › doc › 2.4 › reference › generated › numpy.where.html
numpy.where — NumPy v2.4 Manual
numpy.where(condition, [x, y, ]/)# Return elements chosen from x or y depending on condition. Note · When only condition is provided, this function is a shorthand for np.asarray(condition).nonzero(). Using nonzero directly should be preferred, as it behaves correctly for subclasses.