Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.where.html
pandas.DataFrame.where — pandas 3.0.6 documentation
Where cond is True, keep the original value. Where False, replace with corresponding value from other. If cond is callable, it is computed on the Series/DataFrame and should return boolean Series/DataFrame or array. The callable must not change input Series/DataFrame (though pandas doesn’t ...
W3Schools
w3schools.com › python › pandas › ref_df_where.asp
Pandas DataFrame where() Method
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS
11:19
Pandas where() Explained with Multiple Examples - YouTube
08:21
Search-and-replace Pandas values with "where" and "mask" - YouTube
Pandas Where | pd.DataFrame.where()
05:19
How to Use where() in Numpy and Pandas (Python) - YouTube
01:34:11
Complete Python Pandas Data Science Tutorial! (2024 Updated Edition) ...
06:40
Very Useful Pandas Functions: Pandas DataFrame mask() vs where() ...
GeeksforGeeks
geeksforgeeks.org › python › python-pandas-dataframe-where
Pandas DataFrame.where()-Python - GeeksforGeeks
June 24, 2025 - import pandas as pd import numpy as np df = pd.DataFrame({'A': [1, -2, 3], 'B': [-1, 5, -6]}) res = df.where(df > 0) print(res) ... Explanation: df.where(df > 0) keeps values where the condition is True (greater than 0) and replaces others with NaN. DataFrame.where(cond, other=nan, inplace=False, axis=None, level=None, errors='raise', try_cast=False) ... Example 1: In this, we are replacing negative numbers with 0 using the other parameter...
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.DataFrame.where.html
pandas.DataFrame.where — pandas 3.0.5 documentation
Where cond is True, keep the original value. Where False, replace with corresponding value from other. If cond is callable, it is computed on the Series/DataFrame and should return boolean Series/DataFrame or array. The callable must not change input Series/DataFrame (though pandas doesn’t ...
Call: +917738666252
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Top answer 1 of 3
82
Try:
(df['A'] + df['B']).where((df['A'] < 0) | (df['B'] > 0), df['A'] / df['B'])
The difference between the numpy where and DataFrame where is that the default values are supplied by the DataFrame that the where method is being called on (docs).
I.e.
np.where(m, A, B)
is roughly equivalent to
A.where(m, B)
If you wanted a similar call signature using pandas, you could take advantage of the way method calls work in Python:
pd.DataFrame.where(cond=(df['A'] < 0) | (df['B'] > 0), self=df['A'] + df['B'], other=df['A'] / df['B'])
or without kwargs (Note: that the positional order of arguments is different from the numpy where argument order):
pd.DataFrame.where(df['A'] + df['B'], (df['A'] < 0) | (df['B'] > 0), df['A'] / df['B'])
2 of 3
6
pandas 2.2 update: Series.case_when
From pandas 2.2.0, the API provides a pandaic alternative to np.where and np.select.
Using case_when:
cond = (df['A'] < 0) | (df['B'] > 0)
df['C'] = (df['A'] / df['B']).case_when([(cond, df['A'] + df['B'])])
# or
df['C'] = 0 # Or pd.NA or any reasonable default.
df['C'] = df['C'].case_when([(cond, df['A'] + df['B']),
(~cond, df['A'] / df['B']),
])
You notice that case_when allows you to provide an arbitrary list of conditions and replacement pairs, so this can generalize to several conditions easily (much like np.select).
Using np.where:
df['C'] = np.where((df['A'] < 0) | (df['B'] > 0), df['A'] + df['B'], df['A'] / df['B'])
Pandas
pandas.pydata.org › pandas-docs › version › 0.22 › generated › pandas.DataFrame.where.html
pandas.DataFrame.where — pandas 0.22.0 documentation
For further details and examples see the where documentation in indexing.
w3resource
w3resource.com › pandas › dataframe › dataframe-where.php
Pandas DataFrame: where() function - w3resource
Pandas DataFrame - where() function: The where() function is used to replace values where the condition is False.
Statology
statology.org › home › pandas: how to use equivalent of np.where()
Pandas: How to Use Equivalent of np.where()
June 24, 2022 - And here’s the basic syntax using the pandas where() function: df['col'] = (value_if_false).where(condition, value_if_true) The following example shows how to use the pandas where() function in practice.
Educative
educative.io › answers › what-is-pandas-dataframewhere-in-python
What is Pandas DataFrame.where() in Python?
Line 10: We invoke the DataFrame() method from the Pandas package to convert this nested list into a DataFrame of Name, Class, and Marks.
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.query.html
pandas.DataFrame.query — pandas 3.0.6 documentation
See also the Python documentation about lexical analysis in combination with the source code in pandas.core.computation.parsing. Examples · >>> df = pd.DataFrame( ... {"A": range(1, 6), "B": range(10, 0, -2), "C&C": range(10, 5, -1)} ... ) >>> df A B C&C 0 1 10 10 1 2 8 9 2 3 6 8 3 4 4 7 4 5 2 6 >>> df.query("A > B") A B C&C 4 5 2 6 ·
Medium
medium.com › @stacymacbrains › pandas-where-vs-numpy-where-df68efcb580f
Pandas where vs. NumPy where. Pandas where and NumPy where are… | by Ogochukwu Stanley Ikegbo | Medium
November 25, 2024 - Example: import numpy as np · arr = np.array([1, 2, 3, 4, 5]) result = np.where(arr > 3, 'Large', 'Small') print(result) Key Differences: Data Structures: Pandas where operates on DataFrames, while NumPy where operates on NumPy arrays. Flexibility: NumPy where is more flexible and can be used for various array operations beyond simple conditional replacement.
Pandas
pandas.pydata.org › docs › user_guide › 10min.html
10 minutes to pandas — pandas 3.0.6 documentation
pandas provides various facilities for easily combining together Series and DataFrame objects with various kinds of set logic for the indexes and relational algebra functionality in the case of join / merge-type operations.