Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据条件替换数据帧值_Python_Pandas - Fatal编程技术网

Python 根据条件替换数据帧值

Python 根据条件替换数据帧值,python,pandas,Python,Pandas,我有一个主数据帧,df: Colour Item Price Blue Car 40 Red Car 30 Green Truck 50 Green Bike 30 然后我有一个价格修正数据帧,df_pc: Colour Item Price Red Car 60 Green Bike 70 我想说的是,如果价格修正数据框中的颜色和项目匹配,则替换主df中的价格。因此,预期产出为: Colour Item Price Bl

我有一个主数据帧,df:

Colour Item   Price
Blue   Car     40
Red   Car     30
Green  Truck   50
Green  Bike    30
然后我有一个价格修正数据帧,df_pc:

Colour Item   Price
Red   Car     60
Green  Bike    70
我想说的是,如果价格修正数据框中的颜色和项目匹配,则替换主df中的价格。因此,预期产出为:

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

我目前找不到这样做的方法

这里有一种方法使用
combine\u first()


编辑: 如果您只需要匹配的项目,我们还可以使用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))

用于筛选出不匹配的行,然后:

另一个数据测试:

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
打印(df_pc)
彩色物品价格
0红色轿车60

1橙色自行车70首先,想想如果比赛不止一场,该怎么办。假设没有,您可以合并关键颜色/项目上的两个数据框,然后用第一个数据框中的值填充合并数据框列中的空白。如果您需要确切的代码帮助,请发布生成数据帧的工作代码示例。好的,谢谢,是的,没有重复。你认为这是最好的方法吗?这里有
df.drop_duplicates(keep='first',subset='color',Item'])
。这太棒了。我可以检查一下,如果df_pc中有一个不必要的行,那么它就被忽略了(并且无论如何都不会添加到master中)?啊,我明白了,这就是你的编辑所做的。非常感谢
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
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
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