Home > database >  replace column values with values from different dataframe
replace column values with values from different dataframe

Time:11-24

I have 2 pandas dataframes:

df1

     Home  Place
a    MS    Z2
c    KM    Z3
d    RR    R2

df2

     Place1
a    A2      
c    A66
z    F32
x    K41
t    E90

I want to replace values of df2['Place1'] with df1['Place'] when indexes are matching and leave it the same when indexes are not matching.

Desired result:

     Place1
a    Z2 
c    Z3
z    F32
x    K41
t    E90

I tried to use pd.replace but it returns NAs

CodePudding user response:

Try with update

df2['Place1'].update(df1['Place'])
df2
Out[75]: 
  Place1
a     Z2
c     Z3
z    F32
x    K41
t    E90

CodePudding user response:

You can do this with update.

df2['Place1'] = df2['Place1'].update(df1['Place'])
  • Related