Mustafa Moiz
Mustafa Moiz

Reputation: 53

Updating multiple columns in dataframe with columns from another dataframe

I have a dataframe, df1, which certain columns having null values. To get the null values, I created another dataframe, df2, and in df2 I made those values non-null. I'll give an example of my dataframes here, bear in mind this is just an example as the actual dataframes are too long and this is simplified.

df1
   Col1 Col2 Col3
0    A    B    C    
1    D    E    F    
2  NaN    G  NaN     
3  NaN    H  NaN

df2
       Col1 Col3
    0    A    C    
    1    D    F    
    2    I    J     
    3    K    L

Now to replace a single column in df1, I can just use the following code

df1[Col1] = df2[Col1]

But how can I replace multiple columns like this at once? Is there a way I could replace Col1 and Col3 at the same time?

I tried some solutions from similar questions but they didn't work with mine.

Upvotes: 1

Views: 115

Answers (1)

BENY
BENY

Reputation: 323236

What I will do is update

df1.update(df2)

Upvotes: 2

Related Questions