这可能是一个基本问题,但我无法找到解决方案。我有两个数据帧,具有相同的行和列,称为卷和价格,就像这样
卷
Index ProductA ProductB ProductC ProductD Limit
0 100 300 400 78 100
1 110 370 20 30 100
2 90 320 200 121 100
3 150 320 410 99 100
....
价格
Index ProductA ProductB ProductC ProductD Limit
0 50 110 30 90 0
1 51 110 29 99 0
2 49 120 25 88 0
3 51 110 22 96 0
....
我想将0分配给" cell"价格数据框对应的卷数小于限制列
上的数量所以,理想的输出是
价格
Index ProductA ProductB ProductC ProductD Limit
0 50 110 30 0 0
1 51 110 0 0 0
2 0 120 25 88 0
3 51 110 22 0 0
....
我试过
import pandas as pd
import numpy as np
d_price = {'ProductA' : [50, 51, 49, 51], 'ProductB' : [110,110,120,110],
'ProductC' : [30,29,25,22],'ProductD' : [90,99,88,96], 'Limit': [0]*4}
d_volume = {'ProductA' : [100,110,90,150], 'ProductB' : [300,370,320,320],
'ProductC' : [400,20,200,410],'ProductD' : [78,30,121,99], 'Limit': [100]*4}
Prices = pd.DataFrame(d_price)
Volumes = pd.DataFrame(d_volume)
Prices[Volumes > Volumes.Limit]=0
但我没有对价格数据框进行任何更改......显然我很难理解布尔切片,任何帮助都会很棒
答案 0 :(得分:1)
问题在于
Prices[Volumes > Volumes.Limit]=0
由于每行的限制不同,您应该使用,例如,应用如下:
Prices[Volumes.apply(lambda x : x>x.Limit, axis=1)]=0
答案 1 :(得分:0)
你可以使用面具来解决这个问题,我也不是专家,但这个解决方案可以做你想做的事。
test=(Volumes.ix[:,'ProductA':'ProductD'] >= Volumes.Limit.values)
final = Prices[test].fillna(0)