简体   繁体   中英

Pandas replace dataframe values based on criteria

I have a master dataframe, df:

Colour Item   Price
Blue   Car     40
Red   Car     30
Green  Truck   50
Green  Bike    30

I then have a price correction dataframe, df_pc:

Colour Item   Price
Red   Car     60
Green  Bike    70

I want to say if there is a match on Colour and Item in the price correction dataframe, then replace the price in the master df. so the expected output is;

Colour Item   Price
Blue   Car     60
Red   Car     30
Green  Truck   50
Green  Bike    70

I can't find a way of doing this currently

Use Index.isin for filter out no matched rows and then DataFrame.combine_first :

df = df.set_index(['Colour','Item'])
df_pc = df_pc.set_index(['Colour','Item'])

df_pc = df_pc[df_pc.index.isin(df.index)]
df = df_pc.combine_first(df).reset_index()
print (df)
  Colour   Item  Price
0   Blue    Car   40.0
1  Green   Bike   70.0
2  Green  Truck   50.0
3    Red    Car   60.0

Another data test:

print (df_pc)
   Colour  Item  Price
0     Red   Car     60
1  Orange  Bike     70 <- not matched row

df = df.set_index(['Colour','Item'])
df_pc = df_pc.set_index(['Colour','Item'])
df_pc = df_pc[df_pc.index.isin(df.index)]
df = df_pc.combine_first(df).reset_index()
print (df)
  Colour   Item  Price
0   Blue    Car   40.0
1  Green   Bike   30.0
2  Green  Truck   50.0
3    Red    Car   60.0

here is a way using combine_first() :

df_pc.set_index(['Colour','Item']).combine_first(
       df.set_index(['Colour','Item'])).reset_index()

  Colour   Item  Price
0   Blue    Car   40.0
1  Green   Bike   70.0
2  Green  Truck   50.0
3    Red    Car   60.0

EDIT: If you want only matching items, we can also use merge with fillna:

print(df_pc)

  Colour  Item  Price
0     Red   Car     60
1  Orange  Bike     70 #changed row not matching

(df.merge(df_pc, on = ['Colour','Item'],how='left',suffixes=('_x',''))
   .assign(Price=lambda x:x['Price'].fillna(x['Price_x'])).reindex(df.columns,axis=1))

  Colour   Item  Price
0   Blue    Car   40.0
1    Red    Car   60.0
2  Green  Truck   50.0
3  Green   Bike   30.0

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