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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments