简体   繁体   中英

How to replace the values in a dataframe column based on another dataframe condition

I have two dataframe, XXX and override.

XXX = pd.DataFrame({'A':['One', 'Two', 'Three'], 'B': [6,4,3], 'C': ['red','green','blue']})

override = pd.DataFrame({'A':['One','Two'], 'C': ['apple','pie']})

I'm looking for the best way to replace the values ​​of column C of the XXX dataframe where the values ​​of column A of the override dataframe are equal to the values ​​in column A of the dataframe XXX.

I tried to use XXX ['C'] = XXX.merge (override, on = "A"). C_y but the 'blue' value of the "Three" line is replaced with NaN but I want to preserve the original 'blue' value.

Wwhat are the best and most efficient methods to do this using the A field as a key, which is XXX.A = override.A. Thank you very much

You could use map and fillna over series mapper

In [1077]: XXX.A.map(override.set_index('A')['C']).fillna(XXX.C)
Out[1077]:
0    apple
1      pie
2     blue
Name: A, dtype: object

In [1078]: XXX.C = XXX.A.map(override.set_index('A')['C']).fillna(XXX.C)

In [1079]: XXX
Out[1079]:
       A  B      C
0    One  6  apple
1    Two  4    pie
2  Three  3   blue

Using update

XXX=XXX.set_index('A')
XXX.update(override.set_index('A'))
XXX
Out[471]: 
       B      C
A              
One    6  apple
Two    4    pie
Three  3   blue
XXX.reset_index()
Out[472]: 
       A  B      C
0    One  6  apple
1    Two  4    pie
2  Three  3   blue

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