熊猫基于多个索引的两个数据帧相减

时间:2018-10-22 02:39:25

标签: python pandas

我有两个df,我想根据多索引减去

profitDf

Company    Product   Amount
Google     Pixel 2   3000
Microsoft  Window 10 4000
Amazon     AWS       10000

costDf

Company    Product    Amount
Google     Pixel 2    10000
Microsoft  Window 10  1000
ASUS       Router     50000

我想生成一个如下所示的数据框

differenceDf

Company    Product    Difference
Google     Pixel 2    -7000
Microsoft  Window 10  3000
Amazon     AWS        10000
ASUS       Router     -50000

我尝试过

profitDf.set_index(['Company','Product']).sub(costDf.set_index(['Company','Product']), fill_value=0).reset_index()

但显示NotImplementedError: merging with both multi-indexes is not implemented

谢谢您的帮助

1 个答案:

答案 0 :(得分:1)

由于它是使用merge的多个索引

pdf.merge(cdf,on=['Company','Product'],how='outer').fillna(0).eval('Diff=Amount_x-Amount_y')
Out[205]: 
     Company   Product  Amount_x  Amount_y     Diff
0     Google    Pixel2    3000.0   10000.0  -7000.0
1  Microsoft  Window10    4000.0    1000.0   3000.0
2     Amazon       AWS   10000.0       0.0  10000.0
3       ASUS    Router       0.0   50000.0 -50000.0

或使用pivot

pdf.pivot(*pdf.columns).sub(cdf.pivot(*cdf.columns),fill_value=0).stack()
Out[218]: 
Company    Product 
ASUS       Router     -50000.0
Amazon     AWS         10000.0
Google     Pixel2      -7000.0
Microsoft  Window10     3000.0
dtype: float64
相关问题