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.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
🌐
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...
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas.dataframe.where() examples
pandas.DataFrame.where() Examples - Spark By {Examples}
October 8, 2024 - pandas.DataFrame.where() function is similar to if-then/if else that is used to check the one or multiple conditions of an expression in DataFrame and
🌐
GeeksforGeeks
geeksforgeeks.org › python-pandas-dataframe-where
Python | Pandas DataFrame.where() - GeeksforGeeks
December 1, 2023 - In this example, data is filtered on the basis of both Team and Age. Only the rows having Team name "Atlanta Hawks" and players having age above 24 will be displayed. ... # importing pandas package import pandas as pd # making data frame from ...
🌐
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 ...
🌐
Statology
statology.org › home › how to use where() function in pandas (with examples)
How to Use where() Function in Pandas (With Examples)
August 10, 2021 - The following code shows how to use the where() function to replace all values that don’t meet a certain condition in an entire pandas DataFrame with a NaN value.
🌐
IONOS
ionos.com › digital guide › websites › web development › python pandas: dataframe where
How to apply conditions in pandas DataFrames with where()
June 26, 2025 - Suppose you have a DataFrame with a company’s sales results, and you only want to display the positive results. Negative results, on the other hand, should be replaced with 0. You can do this with pandas DataFrame.where().
Find elsewhere
🌐
EDUCBA
educba.com › home › software development › software development tutorials › pandas tutorial › pandas dataframe.where()
Pandas DataFrame.where() | Syntax,Parameters and Examples
April 3, 2023 - So the where method in pandas is responsible for searching the pandas data structure like a series or a dataframe on a given condition and replace the remaining elements which do not satisfy the condition with some value.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
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 - Example: Imagine you have a NumPy array of sales data and you want to mark values below a certain threshold as -1. ... If the sales value is greater than 150, then keep the original value.
🌐
Vultr Docs
docs.vultr.com › python › third-party › pandas › DataFrame › where
Python Pandas DataFrame where() - Filter Data Conditionally | Vultr Docs
December 26, 2024 - In this article, you will learn how to effectively utilize the where() function in Python's Pandas library to filter and manipulate data conditionally.
🌐
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 ·
🌐
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.
🌐
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.
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Replace values based on conditions with where(), mask() | note.nkmk.me
January 17, 2024 - This article explains how to replace values based on conditions in pandas. You can perform conditional operations like if then ... or if then ... else ... on DataFrame or Series. Use the where() metho ...