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'])
Answer from Alex on Stack Overflow
🌐
Pandas
pandas.pydata.org › docs › 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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pandas-dataframe-where
Pandas DataFrame.where()-Python - GeeksforGeeks
June 24, 2025 - Pandas · Practice · Django · Flask · Last Updated : 24 Jun, 2025 · DataFrame.where() function replace values in a DataFrame based on a condition. It allows you to keep the original value where a condition is True and replace it with something ...
🌐
W3Schools
w3schools.com › python › pandas › ref_df_where.asp
Pandas DataFrame where() Method
import pandas as pd data = { "age": [50, 40, 30, 40, 20, 10, 30], "qualified": [True, False, False, False, False, True, True] } df = pd.DataFrame(data) newdf = df.where(df["age"] > 30) Try it Yourself »
🌐
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 - Returns an array with elements chosen from either x or y based on the condition. ... Data Structures: Pandas where operates on DataFrames, while NumPy where operates on NumPy arrays.
Find elsewhere
🌐
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 - Else, replace it with -1. ... 📊 pandas.DataFrame.where: The “If-Not” of DataFrames In contrast, pandas.DataFrame.where is used for conditional selection and replacement within Pandas DataFrames or Series.
🌐
Pandas
pandas.pydata.org › docs › user_guide › indexing.html
Indexing and selecting data — pandas 3.0.5 documentation
The Python and NumPy indexing operators [] and attribute operator . provide quick and easy access to pandas data structures across a wide range of use cases. This makes interactive work intuitive, as there’s little new to learn if you already know how to deal with Python dictionaries and ...
🌐
Swdevnotes
swdevnotes.com › python › 2020 › pandas-dataframe-where
Pandas - Dataframe Where function | Software Development Notes
November 1, 2020 - Pandas Dataframe Where method is used to replace data in the dataframe where the condition is false and is not used for filtering data like the Where clause in SQL. Filtering data based on conditions in rows and columns can be done with loc and iloc.
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 2.2 › reference › api › pandas.DataFrame.where.html
pandas.DataFrame.where — pandas 2.2.3 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 ...
🌐
Reddit
reddit.com › r/learnpython › using where clause in pandas df
r/learnpython on Reddit: Using where clause in pandas df
November 14, 2016 - select column_1,column_2 from table where column_2 between x and y; i have to execute the same in pandas data frame · i tried to do: df[df['column_2'] <x and >y] but its saying syntax invalid. Share · Share · Sort by: Best · Open comment sort options · Best · Top ·
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Index.where.html
pandas.Index.where — pandas 3.0.4 documentation
>>> idx = pd.Index(["car", "bike", "train", "tractor"]) >>> idx Index(['car', 'bike', 'train', 'tractor'], dtype='str') >>> idx.where(idx.isin(["car", "train"]), "other") Index(['car', 'other', 'train', 'other'], dtype='str')
🌐
Educative
educative.io › answers › what-is-pandas-dataframewhere-in-python
What is Pandas DataFrame.where() in Python?
The DataFrame.where() method in Python replaces values in a DataFrame where a specified condition is false. By default, it replaces empty fields with NaN values. Note: Pandas DataFrame is a two-dimensional labeled data structure.
🌐
IONOS
ionos.com › digital guide › websites › web development › python pandas: dataframe where
How to apply conditions in pandas DataFrames with where()
June 26, 2025 - You can use pandas DataFrame.where() to apply conditions to values in a DataFrame. Discover how it works with easy-to-follow examples.
🌐
Statology
statology.org › home › pandas: how to use equivalent of np.where()
Pandas: How to Use Equivalent of np.where()
June 24, 2022 - df['col'] = (value_if_false).where(condition, value_if_true) The following example shows how to use the pandas where() function in practice.
🌐
Earth Data Science
earthdatascience.org › home
Select Data From Pandas Dataframes | Earth Data Science - Earth Lab
September 23, 2019 - You can also select data from pandas dataframes without knowing the location of that data within the pandas dataframe, using specific labels such as a column name.
🌐
Pandas
pandas.pydata.org › docs › getting_started › intro_tutorials › 03_subset_data.html
How do I select a subset of a DataFrame? — pandas 3.0.5 documentation
The inner square brackets define a Python list with column names, whereas the outer square brackets are used to select the data from a pandas DataFrame as seen in the previous example.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.query.html
pandas.DataFrame.query — pandas 3.0.5 documentation
DataFrame.query(expr, *, parser='pandas', engine=None, local_dict=None, global_dict=None, resolvers=None, level=0, inplace=False)[source]#
🌐
Apache
spark.apache.org › docs › latest › api › python › reference › pyspark.pandas › api › pyspark.pandas.DataFrame.where.html
pyspark.pandas.DataFrame.where — PySpark 4.1.1 documentation
>>> from pyspark.pandas.config import set_option, reset_option >>> set_option("compute.ops_on_diff_frames", True) >>> df1 = ps.DataFrame({'A': [0, 1, 2, 3, 4], 'B':[100, 200, 300, 400, 500]}) >>> df2 = ps.DataFrame({'A': [0, -1, -2, -3, -4], 'B':[-100, -200, -300, -400, -500]}) >>> df1 A B 0 0 100 1 1 200 2 2 300 3 3 400 4 4 500 >>> df2 A B 0 0 -100 1 -1 -200 2 -2 -300 3 -3 -400 4 -4 -500 · >>> df1.where(df1 > 0).sort_index() A B 0 NaN 100.0 1 1.0 200.0 2 2.0 300.0 3 3.0 400.0 4 4.0 500.0
🌐
Vultr Docs
docs.vultr.com › python › third-party › pandas › DataFrame › where
Python Pandas DataFrame where() - Filter Data Conditionally | Vultr Docs
December 26, 2024 - The where() function in Pandas is a versatile tool designed to filter data in a DataFrame based on a condition.