Maybe there is a more elegant way, but I would do in this way:

df['C'] = df['B'].apply(lambda x: f(x)[0])
df['D'] = df['B'].apply(lambda x: f(x)[1])

Applying the function to the columns and get the first and the second value of the outputs. It returns:

   A  B   C   D
0  1  O  Ok  On
1  2  P  Pk  Pn
2  3  Q  Qk  Qn
3  4  R  Rk  Rn

EDIT:

In a more concise way, thanks to this answer:

df[['C','D']] = df['B'].apply(lambda x: pd.Series([f(x)[0],f(x)[1]]))
Answer from Fabio Lamanna on Stack Overflow
🌐
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 - For example, you may want to update ... condition. To do this, you can use the apply function in Pandas along with a lambda function that checks the condition and returns the updated value....
Discussions

python - Pandas update multiple columns at once - Stack Overflow
I'm trying to update a couple fields at once - I have two data sources and I'm trying to reconcile them. I know I could do some ugly merging and then delete columns, but was expecting this code be... More on stackoverflow.com
🌐 stackoverflow.com
python - Pandas update multiple columns using apply function - Stack Overflow
I am updating a data frame using apply of function. But now I need to modify multiple columns using this function, Here is my sample code: def update_row(row): listy = [1,2,3] return l... More on stackoverflow.com
🌐 stackoverflow.com
August 6, 2018
lambda - Update Multiple Columns using Pandas Apply Function - Stack Overflow
Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams · Get early access and see previews of new features. Learn more about Labs ... Earn badges by improving or asking questions in Staging Ground. See new badges ... I am trying to update the multiple columns using pandas dataframe apply ... More on stackoverflow.com
🌐 stackoverflow.com
April 17, 2019
python - Updating multiple columns using apply function on a single column. The apply function returns two lists - Stack Overflow
This question is in a collective: ... by tags with relevant content and experts. ... Upcoming initiatives on Stack Overflow and across the Stack Exchange network... ... Should we remove font-specific styling tags like [bold], [italic] and... Stack Overflow Experiment: Safely expanding voting access · 5 Apply "list" function on multiple columns pandas · 0 Apply a function across multiple Columns in Pandas · 8 Pandas update multiple columns ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Saturn Cloud
saturncloud.io › blog › how-to-update-multiple-columns-using-pandas-apply-function
How to Update Multiple Columns using Pandas Apply Function | Saturn Cloud Blog
October 4, 2023 - To update multiple columns using the Pandas apply function, you need to define a function that takes a row or column of the DataFrame as an argument and returns a modified version of that row or column.
Top answer
1 of 3
44

you want to replace

print df.loc[df['Col1'].isnull(),['Col1','Col2', 'Col3']]

  Col1 Col2 Col3
2  NaN  NaN  NaN
3  NaN  NaN  NaN

With:

replace_with_this = df.loc[df['Col1'].isnull(),['col1_v2','col2_v2', 'col3_v2']]
print replace_with_this

  col1_v2 col2_v2 col3_v2
2       a       b       d
3       d       e       f

Seems reasonable. However, when you do the assignment, you need to account for index alignment, which includes columns.

So, this should work:

df.loc[df['Col1'].isnull(),['Col1','Col2', 'Col3']] = replace_with_this.values

print df

  Col1 Col2 Col3 col1_v2 col2_v2 col3_v2
0    A    B    C     NaN     NaN     NaN
1    D    E    F     NaN     NaN     NaN
2    a    b    d       a       b       d
3    d    e    f       d       e       f

I accounted for columns by using .values at the end. This stripped the column information from the replace_with_this dataframe and just used the values in the appropriate positions.

2 of 3
2

In the "take the hill" spirit, I offer the below solution which yields the requested result.

I realize this is not exactly what you are after as I am not slicing the df (in the reasonable - but non functional - way in which you propose).

#Does not work when indexing on np.nan, so I fill with some arbitrary value. 
df = df.fillna('AAA')

#mask to determine which rows to update
mask = df['Col1'] == 'AAA'

#dict with key value pairs for columns to be updated
mp = {'Col1':'col1_v2','Col2':'col2_v2','Col3':'col3_v2'}

#update
for k in mp: 
     df.loc[mask,k] = df[mp.get(k)]

#swap back np.nans for the arbitrary values
df = df.replace('AAA',np.nan)

Output:

Col1    Col2    Col3    col1_v2     col2_v2     col3_v2
A       B       C       NaN         NaN         NaN
D       E       F       NaN         NaN         NaN
a       b       d       a           b           d
d       e       f       d           e           f

The error I get if I do not replace nans is below. I'm going to research exactly where that error stems from.

ValueError: array is not broadcastable to correct shape
🌐
Medium
macxima.medium.com › python-how-to-update-multiple-columns-at-once-in-dataframe-ea801f6a70a8
Python — How to update multiple columns at once in dataframe? | by Ryan Arjun | Medium
May 25, 2024 - Working as Python developer, data analysts or data scientists for any organisation then it is very important for you to know how to play with Dataframes. We understand, we can add a column to a dataframe and update its values to the values returned from a function or other dataframe column’s values as given below - # pandas library for data manipulation in python import pandas as pd# create a dataframe with number values df = pd.DataFrame({'Num':[5,10,15,17,22,25,28,32,36,40,50,]})#display values from dataframe df#create square() function to return single value #passing variable is x #return single valuedef square(x): return x*x#Add new column and update value in it df['Square of Num'] = [square(i) for i in df['Num']]#display values from dataframe df
🌐
YouTube
youtube.com › knowledge base
Pandas : Pandas update multiple columns using apply function - YouTube
Pandas : Pandas update multiple columns using apply function [ Beautify Your Computer : https://www.hows.tech/p/recommended.html ] Pandas : Pandas update mu...
Published   February 12, 2022
Views   21
Find elsewhere
🌐
DigitalOcean
digitalocean.com › community › tutorials › update-rows-and-columns-python-pandas
How to Update Rows and Columns Using Python Pandas | DigitalOcean
August 4, 2022 - In this tutorial, we will be focusing ... rows and columns in python using pandas. Without spending much time on the intro, let’s dive into action!. In this whole tutorial, we will be using a dataframe that we are going to create now. This will give you an idea of updating operations on the data. After this, you can apply these methods ...
🌐
CSDN
devpress.csdn.net › python › 62fd96607e66823466192997.html
Pandas update multiple columns at once_python_Mangs-Python
August 18, 2022 - replace_with_this = df.loc[df['Col1'].isnull(),['col1_v2','col2_v2', 'col3_v2']] print replace_with_this col1_v2 col2_v2 col3_v2 2 a b d 3 d e f · Seems reasonable. However, when you do the assignment, you need to account for index alignment, which includes columns.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.DataFrame.update.html
pandas.DataFrame.update — pandas 3.0.1 documentation
Return True for values that should be updated. errors{‘raise’, ‘ignore’}, default ‘ignore’ · If ‘raise’, will raise a ValueError if the DataFrame and other both contain non-NA data in the same place. ... This method directly changes calling object. ... When errors=’raise’ and there’s overlapping non-NA data. When errors is not either ‘ignore’ or ‘raise’ ... Similar method for dictionaries. ... For column(s)-on-column(s) operations.
🌐
Stack Overflow
stackoverflow.com › questions › 29673203 › pandas-update-multiple-fields
python - pandas update multiple fields - Stack Overflow
April 16, 2015 - What I was curious about was being able to use apply to return arbritrarily many fields. By being able to do this it should allow for cleaner code. ... Content Discovery initiative April 13 update: Related questions using a... Review our technical responses for the 2023 Developer Survey · 3696 Catch multiple exceptions in one line (except block) 1678 Selecting multiple columns in a Pandas ...
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-apply-a-function-to-multiple-columns-in-pandas
How to Apply a function to multiple columns in Pandas? - GeeksforGeeks
August 16, 2022 - Pandas.apply allow the users to pass a function and apply it on every single value of the Pandas series. ... In this example, we are passing only a single column and increment age with 2.
🌐
YouTube
youtube.com › mukesh singh
Python - How to Update Multiple Columns in Dataframe - YouTube
In this tutorial, you will learn "How to Update Multiple Columns in Dataframe" in Pandas. For this I used Python 3.12 runtime.Pandas is a powerful and flexib...
Published   May 25, 2024
Views   70
🌐
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)
🌐
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 - # Apply function NumPy.square() ... # A B C #0 9 25 7 #1 4 16 6 #2 25 64 9 ... apply() is a Pandas DataFrame method used to apply a function along the axis of a DataFrame....
🌐
Javaer101
javaer101.com › en › article › 19818669.html
Pandas update multiple columns using apply function - Javaer101
def update_row(row): listy = [1,2,3] return listy dp_data_df[['A', 'P','Y']] = dp_data_df.apply(update_row, axis=1) ... ValueError: shape mismatch: value array of shape (10,) could not be broadcast to indexing result of shape (3,10) Thanks in advance. ... dp_data_df = pd.DataFrame({'A':[3,5,6,6], 'B':[6,7,8,9], 'P':[5,6,7,0], 'Y':[1,2,3,4]}) print (dp_data_df) A B P Y 0 3 6 5 1 1 5 7 6 2 2 6 8 7 3 3 6 9 0 4 def update_row(row): listy = [1,2,3] return pd.Series(listy) dp_data_df[['A', 'P','Y']] = dp_data_df.apply(update_row, axis=1) print (dp_data_df) A B P Y 0 1 6 2 3 1 1 7 2 3 2 1 8 2 3 3 1 9 2 3