🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.where.html
numpy.where — NumPy v2.5 Manual
>>> x, y = np.ogrid[:3, :4] >>> np.where(x < y, x, 10 + y) # both x and 10+y are broadcast array([[10, 0, 0, 0], [10, 11, 1, 1], [10, 11, 12, 2]])
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.where.html
numpy.where — NumPy v2.6.dev0 Manual
[0, 3, 6]]) >>> np.where(a < 4, a, -1) # -1 is broadcast array([[ 0, 1, 2], [ 0, 2, -1], [ 0, 3, -1]])
People also ask

What does np.where return with one argument?
It returns a tuple of index arrays identifying the positions where the condition is true. To retrieve the matching values, use that tuple to index the original array, or use boolean indexing directly when you don't need the positions separately.
🌐
askpython.com
askpython.com › python-modules › numpy › python-numpy-where
np.where in Python: Find Indices and Replace Values with ...
Why does np.where return a tuple?
Each array in the tuple describes one input dimension, so a two-dimensional condition produces separate row and column arrays with corresponding entries. A one-dimensional condition still returns a tuple, which is why its printed result contains a trailing comma after the index array.
🌐
askpython.com
askpython.com › python-modules › numpy › python-numpy-where
np.where in Python: Find Indices and Replace Values with ...
How do you use multiple conditions with np.where?
Parenthesize each comparison and combine the resulting boolean masks with &amp; or |, using ~ when you need to invert a mask. For numerical data with missing entries, explicitly decide how to handle NaN because ordinary range comparisons return False at those positions.
🌐
askpython.com
askpython.com › python-modules › numpy › python-numpy-where
np.where in Python: Find Indices and Replace Values with ...
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.where.html
numpy.where — NumPy v2.2 Manual
>>> x, y = np.ogrid[:3, :4] >>> np.where(x < y, x, 10 + y) # both x and 10+y are broadcast array([[10, 0, 0, 0], [10, 11, 1, 1], [10, 11, 12, 2]])
🌐
Medium
medium.com › @kelvinsang97 › python-np-where-97bdbdcf9eab
Python np.where(). This function can be used to select… | by Kelvin Kipsang | Medium
February 9, 2023 - df = df.replace(np.NaN,value=0) df Amount Account 0 200.0 Operation 1 212.0 Finace 2 0.0 Marketing 3 300.0 Operation 4 500.0 Finace 5 0.0 Marketing · Now we begin applying the np.where() function to categorizing column values into atmost two unique row values.
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
numpy.where(): Manipulate elements depending on conditions | note.nkmk.me
November 5, 2019 - If you know it is one-dimensional, you can use the first element of the result of np.where() as it is. In this case, it will be a ndarray with an integer int as an element, not a tuple with one element.
🌐
Statology
statology.org › home › pandas: how to use equivalent of np.where()
Pandas: How to Use Equivalent of np.where()
June 24, 2022 - For example, the following code shows how to update the values in a NumPy array that meet a certain condition: import numpy as np #create NumPy array of values x = np.array([1, 3, 3, 6, 7, 9]) #update valuesin array based on condition x = np.where((x < 5) | (x > 8), x/2, x) #view updated array x array([0.5, 1.5, 1.5, 6.
🌐
Plus2Net
plus2net.com › python › numpy-where.php
NumPy where(): Conditions, Positions and Practical Examples | plus2net
np.where(condition, x, y) chooses values from x where a condition is True and from y elsewhere. With only the condition, it returns matching index arrays instead. Learn the difference before filtering, replacing missing values or assigning ...
🌐
Python Guides
pythonguides.com › python-numpy-where
Optimizing Data Analysis in Pandas Using np.where() in 2025
May 8, 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
Find elsewhere
🌐
AskPython
askpython.com › python-modules › numpy › python-numpy-where
np.where in Python: Find Indices and Replace Values with numpy.where() - AskPython
September 29, 2026 - Call np.where with only a condition and it answers with a tuple of index positions, not the values themselves. That shape catches you on the first line you print, and the trailing comma inside the parentheses is the clue to why.
🌐
Stack Exchange
datascience.stackexchange.com › questions › 88922 › why-is-np-where-not-returning-1-only-returns-0
python - Why is np.where not returning '1'? Only returns '0' - Data Science Stack Exchange
February 4, 2021 - You are first checking if the item contains the string, but then in np.where you are checking if the values are equal (item == s), which is obviously different. In addition, you set the whole column equal to the value from np.where (and overwriting it after each row), which results in the whole column getting the value based on the final row of the dataframe.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.where.html
pandas.DataFrame.where — pandas 3.1.1 documentation
>>> df = pd.DataFrame(np.arange(10).reshape(-1, 2), columns=["A", "B"]) >>> df A B 0 0 1 1 2 3 2 4 5 3 6 7 4 8 9 >>> m = df % 3 == 0 >>> df.where(m, -df) A B 0 0 -1 1 -2 3 2 -4 -5 3 6 -7 4 -8 9 >>> df.where(m, -df) == np.where(m, df, -df) A B 0 True True 1 True True 2 True True 3 True True 4 True True >>> df.where(m, -df) == df.mask(~m, -df) A B 0 True True 1 True True 2 True True 3 True True 4 True True
🌐
Towards Data Science
towardsdatascience.com › home › artificial intelligence › the difference between where and select functions of python numpy
The Difference Between Where and Select Functions of Python NumPy | Towards Data Science
August 15, 2021 - The where function is able to evaluate values for one set of conditions. As a result, we can only assign two different values in the new column: one for the rows that fit the conditions and one for the others.
🌐
CASRAI
casrai.org › guides › research tools & software › statistical software for research › numpy where: syntax, multiple conditions and np.select
NumPy where: Syntax, Multiple Conditions and np.select
1 week ago - NumPy where explained: np.where(condition, x, y), multiple conditions with & and |, np.select for 3+ outcomes, and the nonzero form, with outputs.
🌐
Medium
medium.com › analytics-vidhya › demystifying-np-where-and-np-select-76e3ca49e316
Demystifying np.where and np.select | by Siddharth Kshirsagar | Analytics Vidhya | Medium
January 18, 2024 - np.where(a < 4,a,-1) Out[8]: array([[ 0, 1, 2], [ 0, 2, -1], [ 0, 3, -1]]) ... If x and y are not specified, the value returned by the where function are the indices of the elements satisfying the condition.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.DataFrame.where.html
pandas.DataFrame.where — pandas 3.1.0 documentation
>>> df = pd.DataFrame(np.arange(10).reshape(-1, 2), columns=["A", "B"]) >>> df A B 0 0 1 1 2 3 2 4 5 3 6 7 4 8 9 >>> m = df % 3 == 0 >>> df.where(m, -df) A B 0 0 -1 1 -2 3 2 -4 -5 3 6 -7 4 -8 9 >>> df.where(m, -df) == np.where(m, df, -df) A B 0 True True 1 True True 2 True True 3 True True 4 True True >>> df.where(m, -df) == df.mask(~m, -df) A B 0 True True 1 True True 2 True True 3 True True 4 True True