简体   繁体   中英

replace column values with values from different dataframe

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

Try with update

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

You can do this with update .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM