is that what you want?

In [300]: frame[['b','c']].apply(lambda x: x['c'] if x['c']>0 else x['b'], axis=1)
Out[300]:
0   -1.099891
1    0.582815
2    0.901591
3    0.900856
dtype: float64
Answer from MaxU - stand with Ukraine on Stack Overflow
🌐
Data to Fish
datatofish.com › if-condition-in-pandas-dataframe
Two Ways to Apply an If-Condition on a pandas DataFrame
df.loc[df['column'] == condition_value, 'target_column' ] = then_value df['target_column'] = df['column'].apply(lambda x: then_value if x == condition_value)
Discussions

python - Conditional Logic on Pandas DataFrame - Stack Overflow
How to apply conditional logic to a Pandas DataFrame. See DataFrame shown below, data desired_output 0 1 False 1 2 False 2 3 True 3 4 Tru... More on stackoverflow.com
🌐 stackoverflow.com
How to write a lambda function that is conditional on two variables (columns) in python - Stack Overflow
Using StackOverflow and the ... than one column (using the axis option). Please help. ... This will be much faster than performing an apply operation as it is vectorised. ... Sign up to request clarification or add additional context in comments. ... This is exactly what I needed. And this is great because I can already see how I can expand it to conditionals on 3 or more variables. Thank you! 2014-07-17T22:43:37.72Z+00:00 ... Lambda(s) in Python ... More on stackoverflow.com
🌐 stackoverflow.com
how to update a pandas dataframe column value, when a specific string appears in another column?
It's not something you'd really use .apply for. You would use boolean indexing, e.g. df['A'].str.contains('foo') would give you a Series of True/False values. You can then use .loc to set column(s) to a particular value for the True rows: df.loc[df['A'].str.contains('foo'), 'B'] = 'bar' More on reddit.com
🌐 r/learnpython
7
3
June 24, 2024
python - Using Apply in Pandas Lambda functions with multiple if statements - Stack Overflow
I'm trying to infer a classification according to the size of a person in a dataframe like this one: Size 1 80000 2 8000000 3 8000000000 ... I want it to look like this: S... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Towards Data Science
towardsdatascience.com › home › latest › 5 ways to apply if-else conditional statements in pandas
5 Ways to Apply If-Else Conditional Statements in Pandas | Towards Data Science
January 28, 2025 - The new column ‘visits_category’ ... 'YES', 'NO') ... Like np.where() , the lambda function is another superb choice when you need to add a column based on a simple binary if-else condition....
🌐
GeeksforGeeks
geeksforgeeks.org › python › using-apply-in-pandas-lambda-functions-with-multiple-if-statements
Using Apply in Pandas Lambda functions with multiple if statements - GeeksforGeeks
June 20, 2025 - Lambda functions in apply() are ideal for simple conditions such as pass/fail classification. ... import pandas as pd df = pd.DataFrame({'Name': ['John', 'Jack', 'Shri', 'Krishna', 'Smith', 'Tessa'], 'Maths': [5, 3, 9, 10, 6, 3]}) # Adding the result column df['Result'] = df['Maths'].apply(lambda x: 'Pass' if x>=5 else 'Fail') print(df)
🌐
GeeksforGeeks
geeksforgeeks.org › python › ways-to-apply-an-if-condition-in-pandas-dataframe
How to apply if condition in Pandas DataFrame - GeeksforGeeks
July 15, 2025 - import pandas as pd # Sample DataFrame data = {'Name': ['John', 'Sophia', 'Daniel', 'Emma'], 'Experience': [5, 8, 3, 10]} df = pd.DataFrame(data) print("Original Dataset") display(df) # Apply if condition using lambda function df['Category'] = df['Experience'].apply(lambda x: 'Senior' if x >= 5 else 'Junior') print("Dataset with 'Senior'and 'Junior' Category") display(df) ... For conditional logic, np.where() is often faster than apply() and can be used to return one value when the condition is true, and another when it's false.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.apply.html
pandas.DataFrame.apply — pandas 3.0.1 documentation
The resulting column names will be the originals. >>> df.apply(lambda x: [1, 2], axis=1, result_type="broadcast") A B 0 1 2 1 1 2 2 1 2 · Advanced users can speed up their code by using a Just-in-time (JIT) compiler with apply. The main JIT compilers available for pandas are Numba and Bodo.
Find elsewhere
🌐
ProjectPro
projectpro.io › blog › how to apply lambda functions to python pandas?
How To Apply Lambda Functions To Python Pandas?
October 28, 2024 - You can use Lambda functions in Pandas to apply conditional logic to data. This can be done by using nested if-else statements within the lambda function. For example, the following Lambda if-else Pandas function checks if a value is less than ...
🌐
Reddit
reddit.com › r/learnpython › how to update a pandas dataframe column value, when a specific string appears in another column?
r/learnpython on Reddit: how to update a pandas dataframe column value, when a specific string appears in another column?
June 24, 2024 -

So, i've figured out how to use the pandas apply method to update/change the values of a column, row-wise based on multiple comparisons like this:

# for each row, if the value of both 'columns to check' are 'SOME STRING', change to 'NEW STRING
# otherwise leave it as is
my_df ['column_to_change'] = df.apply(lambda row: 'NEW STRING' if row['column_to_check_1'] and row['column_to_check_2'] == 'SOME STRING' else row['column_to_change'], axis=1)

Now, I can't figure out how to expand that beyond simple comparison operators. The specific example I'm trying to solve is:

" for each row, if the string value in COLUMN A contains 'foo', change the value in COLUMN B to 'bar', otherwise leave it as is"

I think this is all right, except the ##parts between the hashmarks##

my_df ['columb_b'] = df.apply(lambda row: 'bar' if ##column A contains 'foo'## else row['columb_b'], axis=1)
🌐
Statology
statology.org › home › pandas: how to use apply & lambda together
Pandas: How to Use Apply & Lambda Together
June 23, 2022 - You can use the following basic syntax to apply a lambda function to a pandas DataFrame: df['col'] = df['col'].apply(lambda x: 'value1' if x < 20 else 'value2')
🌐
Saturn Cloud
saturncloud.io › blog › how-to-use-ifelse-function-in-pandas-dataframe
How to Use If-Else Function in Pandas DataFrame | Saturn Cloud Blog
November 10, 2023 - The apply() function applies a function along an axis of the DataFrame. The lambda function is a short, anonymous function that takes in a value and returns a value based on a certain condition.
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas apply() function to single & multiple column(s)
Pandas apply() Function to Single & Multiple Column(s) - Spark By {Examples}
December 6, 2024 - Use apply() and a lambda function to conditionally square the values of columns A and B using NumPy’s square() function. For instance, the lambda function checks if the name of the current column (x.name) is either A or B. If so, it applies np.square(x) to square the values in that column.
🌐
Saturn Cloud
saturncloud.io › blog › using-lambda-function-pandas-to-set-column-values
Using Lambda Function Pandas to Set Column Values | Saturn Cloud Blog
January 25, 2024 - To set column values in a Pandas DataFrame, we can use the .apply() function along with a lambda function. The .apply() function applies a function to each element of a DataFrame.
🌐
GeeksforGeeks
geeksforgeeks.org › applying-lambda-functions-to-pandas-dataframe
Applying Lambda functions to Pandas Dataframe - GeeksforGeeks
August 9, 2024 - In Python Pandas, we have the freedom to add different functions whenever needed like lambda function, sort function, etc. We can apply a lambda function to both the columns and rows of the Pandas data frame.Syntax: lambda arguments: expressionAn ...
🌐
Medium
medium.com › @whyamit101 › using-pandas-lambda-if-else-0d8368b70459
Using pandas lambda if else. The biggest lie in data science? That… | by why amit | Medium
April 12, 2025 - You can nest if statements within ... to an entire DataFrame? You can use the .apply() method combined with your lambda function to apply it to an entire DataFrame column....
🌐
Delft Stack
delftstack.com › home › howto › python pandas › apply lambda functions to pandas dataframe
How to Apply Lambda Function to Pandas DataFrame | Delft Stack
February 2, 2024 - We used the conditional statement ... on the Monthly Income column. If the monthly income is greater and equal to 5000, add Stable inside the Category column; otherwise, add UnStable....