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

Ig_Ferr

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

Zero

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.

edited at
0

Comments

0 comments
Login to comment

Related

Pandas DataFrame: replace all values in a column, based on condition

Replace values in a dataframe column based on condition

Pandas: replace values in dataframe with another dataframes values based on condition

Replace values in pandas dataframe column with different replacement dict based on condition

How to replace a dataframe column values with NaN based on a condition?

Replace values in dataframe column depending on another column with condition

How to replace pandas dataframe values based on lookup values in another dataframe?

Replace dataframe column values based on matching id in another dataframe

Adding a new column to a dataframe from the values of another dataframe based on a condition

How to append a column to a dataframe with values based on condition

Replace values of a column based on another column having as input a dataframe

Replace certain values of dataframe with values in another dataframe, based on a condition

How to assign values to a column of a dataframe based on a condition?

How to replace values in a dataframe with values in another dataframe based on certain condition?

Replace list with another list in dataframe based on another column condition

Conditionally replace column names in a dataframe based on values in another dataframe

How to Replace values in a pd dataframe column based on a condition with a range of values?

Replace values in a column based on another dataframe

How to replace NaN value in column in Dataframe based on values from another column in same dataframe

How to select a column's values from a dataframe based on a datetime condition in another dataframe?

assign values of one dataframe column to another dataframe column based on condition

How to match column value in a dataframe based on condition in another dataframe?

How to update a Dataframe values from an another Dataframe based on condition

pandas: replace values in a column based on a condition in another dataframe if that value is in the second dataframe

How to replace column values with values in another column based on condition?

How to Replace Dataframe Column Values Based on Condition of Second Dataframe Values

replace values in a column based on a condition in another column and retain remaining values in a dataframe

How to replace the values of a column with another column in pandas dataframe given a condition

Replace values in a dataframe column, based on another dataframe