2014-10-31 95 views
1

这可能是一个基本问题,但我一直无法找到解决方案。我有两个dataframes,具有相同的行和列,被称为量和价格,这都是这个样子在另一列中过滤熊猫数据帧

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价格数据框的“单元格”对应的卷小于极限列上的数值

所以,理想的输出将

价格

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 

,但我不获得对价格数据帧的任何变化......很明显,我有一个很难理解布尔切片,任何帮助将是伟大的

回答

1

问题是在

Prices[Volumes > Volumes.Limit]=0 

由于限制在每行不同,你应该使用,例如,应用于像以下:

Prices[Volumes.apply(lambda x : x>x.Limit, axis=1)]=0 
0

你可以用面膜来解决这个问题,我不是专家,要么但这种解决方案做你想做的事情。

test=(Volumes.ix[:,'ProductA':'ProductD'] >= Volumes.Limit.values) 
final = Prices[test].fillna(0)