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' Answer from commandlineluser on reddit.com
🌐
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)
Discussions

python - Using Lambda Function Pandas to Set Column Values - Stack Overflow
Could anyone suggest a way answer the same question (see link) but by using lambda function: Update a dataframe in pandas while iterating row by row More on stackoverflow.com
🌐 stackoverflow.com
python - Update a pandas data frame column using Apply,Lambda and Group by Functions - Data Science Stack Exchange
I have a data frame in the format mentioned in the screenshot below. Column 'Candidate Won' has only 'loss' as the column value for all the rows. I want to update the Column 'Candidate Won' to a va... More on datascience.stackexchange.com
🌐 datascience.stackexchange.com
June 6, 2020
pandas - use .apply() function to change values to a column of the dataframe - Data Science Stack Exchange
I have a dataframe which is the following: and I would like to consider only the column of instructions and keep just the values push, test, mov, test ,....., so just the first word of each string ... More on datascience.stackexchange.com
🌐 datascience.stackexchange.com
October 31, 2019
python - Pandas change column value based on other column with lambda function - Stack Overflow
Trying to replicate a simple Excel function in pandas, with no success. Haven't tried np.where() yet, as I want to learn lambda functions and rely less on imports where possible. Function to replic... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 - We can use a lambda function inside the .apply() function to set column values based on certain conditions. Let’s take a look at an example. Suppose we have a DataFrame df with columns A, B, and C. We want to set the values in column C based ...
🌐
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')
🌐
YouTube
youtube.com › watch
How to Change Values in a DataFrame Column Using Lambda without Overwriting Existing Data - YouTube
Discover a simple method to use lambda functions with pandas to modify column values conditionally, while preserving existing data in a DataFrame.---This vid...
Published   April 16, 2025
Views   0
🌐
Stack Exchange
datascience.stackexchange.com › questions › 75556 › update-a-pandas-data-frame-column-using-apply-lambda-and-group-by-functions
python - Update a pandas data frame column using Apply,Lambda and Group by Functions - Data Science Stack Exchange
June 6, 2020 - df_andhrapradesh['Candidate Won']=df_andhrapradesh['% of Votes'].apply(lambda x:"Won" if x==df_andhrapradesh.groupby('Constituency')['% of Votes'].max() else "Loss") ... Srujan K.N. 4511 gold badge22 silver badges1010 bronze badges $\endgroup$ 1 · $\begingroup$ You want to change the column "Candidate Won" value to won if the '% of votes' column is maximum in each group where grouping based on 'Constituency' column, right? $\endgroup$ ... I used 'Apply' function to every row in the pandas data frame and created a custom function to return the value for the 'Candidate Won' Column using data frame,row-level 'Constituency','% of Votes'
🌐
Saturn Cloud
saturncloud.io › blog › how-to-update-column-values-in-pandas-based-on-criteria-from-another-column
How to Update Column Values in Pandas Based on Criteria From Another Column | Saturn Cloud Blog
January 18, 2024 - In some cases, you may need to ... column. You can do this by modifying the lambda function to return a tuple of updated values, and then assigning the tuple to the corresponding columns....
Find elsewhere
🌐
CopyProgramming
copyprogramming.com › howto › pandas-change-values-in-column-based-on-condition-lambda
Python: Modifying column values in pandas using a lambda function based on a condition
September 15, 2023 - My goal is to assign a value of 1 to every cell that matches the highest value found in the other columns of the same row. ... df_ref['max'] = df_ref.max(axis=1) df_ref['col1'] = df_ref.col1.apply(lambda x:1 if (x==df_ref['max']) else 0) ... You are close to the solution.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-replace-values-in-columns-based-on-condition-in-pandas
How to Replace Values in Columns Based on Condition in Pandas
July 10, 2023 - import pandas as pd data = { 'name': ['Alice', 'Bob', 'Charlie', 'David', 'Emily'], 'age': [25, 35, 45, 55, 65], 'gender': ['F', 'M', 'M', 'F', 'F'] } df = pd.DataFrame(data) # Replace gender with 'F' where name starts with 'A' df['gender'] = df.apply(lambda x: 'F' if x['name'].startswith('A') else x['gender'], axis=1) print(df) name age gender 0 Alice 25 F 1 Bob 35 M 2 Charlie 45 M 3 David 55 F 4 Emily 65 F · NumPy's where function provides a vectorized approach for conditional replacement ? import pandas as pd import numpy as np data = { 'name': ['Alice', 'Bob', 'Charlie', 'David', 'Emily'], 'age': [25, 35, 45, 55, 65], 'gender': ['F', 'M', 'M', 'F', 'F'] } df = pd.DataFrame(data) # Replace age with 0 where gender is 'M', keep original age otherwise df['age'] = np.where(df['gender'] == 'M', 0, df['age']) print(df)
🌐
Towards Data Science
towardsdatascience.com › home › latest › manipulating values in pandas dataframes
Manipulating Values in Pandas DataFrames | Towards Data Science
January 23, 2025 - Each element in the selected dataframe will be passed as argument into the lambda function. The values in the specified columns are now rounded to 2 decimal places: I hope this article has made it clear for you to decide when you should use the map(), apply(), or applymap() functions. In summary: If you want to modify a single column in a dataframe, use map()
🌐
GeeksforGeeks
geeksforgeeks.org › applying-lambda-functions-to-pandas-dataframe
Applying Lambda functions to Pandas Dataframe - GeeksforGeeks
August 9, 2024 - In this example, we will apply the lambda function Dataframe.assign() to a single column. The function is applied to the 'Total_Marks' column, and a new column 'Percentage' is formed with its help.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-replace-values-in-column-based-on-condition-in-pandas
How to Replace Values in Column Based on Condition in Pandas? - GeeksforGeeks
November 15, 2024 - import pandas as pd # Data Student = { 'Name': ['John', 'Jay', 'sachin', 'Geetha', 'Amutha', 'ganesh'], 'gender': ['male', 'male', 'male', 'female', 'female', 'male'], 'math score': [50, 100, 70, 80, 75, 40], 'test preparation': ['none', 'completed', 'none', 'completed', 'completed', 'none'], } # Creating a DataFrame object df = pd.DataFrame(Student) # Replacing 'female' with 0 using apply and lambda df['gender'] = df['gender'].apply(lambda x: 0 if x == 'female' else x) print(df) ... Name gender math score test preparation 0 John male 50 none 1 Jay male 100 completed 2 sachin male 70 none 3 Geetha 0 80 completed 4 Amutha 0 75 completed 5 ganesh male 40 none · In this article, we’ve explored four effective methods to replace values in a Pandas DataFrame column based on conditions: using loc[], np.where(), masking, and apply() with a lambda function.
🌐
Codecademy
codecademy.com › learn › decp-data-processing-pandas › modules › decp-modifying-data-frames-with-pandas › cheatsheet
Python Pandas for Data Engineers: Modifying DataFrames with Pandas Cheatsheet | Codecademy
# Apply this function to double every value in a specified column · df.column1 = df.column1.apply(double) # Lambda functions can also be supplied to `apply()` df.column2 = df.column2.apply(lambda x : 3*x) # Applying to a row requires it to be called on the entire DataFrame · df['newColumn'] = df.apply(lambda row: row['column1'] * 1.5 + row['column2'], axis=1 · ) Copy to clipboard · Copy to clipboard · Pandas DataFrames allow for the addition of columns after the DataFrame has already been created, by using the format df['newColumn'] and setting it equal to the new column’s value.