我在Pandas日期框架中有小型市场产品销售数据:
date product name turnover qty receipts_qty profit
2015-42 2889220 milk 17.73 3.000 3 13.2700
2015-42 2905772 bread 40.94 2.000 2 40.9400
2015-42 2876012 salt 291.36 5.000 3 292.2450
2015-42 2846814 cakes 92.43 4.000 4 55.5300
...
2015-46 2889220 milk 12.44 2.000 2 9.9344
2015-46 2905772 bread 62.11 3.000 3 62.1100
2015-42 2876012 salt 210.76 4.000 3 190.0050
2015-42 2846814 cakes 120.27 5.000 4 72.1300
[14934 rows x 7 columns]
我需要的是在开始日期和结束日期之间根据'qty'列获得额外的两个数据帧 - 一个用于销售增长的产品,另一个用于销售额下降的产品。像这样:
Increase
name qty_change profit_change
bread 1.000 21.2700
cakes 1.000 16.6000
...
Decrease
name qty_change profit_change
milk -1.000 -3,3356
salt -1.000 -102,2400
...
答案 0 :(得分:1)
您可以尝试:
import pandas as pd
products = pd.DataFrame({'product': [2889220, 2905772, 2876012, 2846814, 2889220, 2905772, 2876012, 2846814],
'date': ['2015-42', '2015-42', '2015-42', '2015-42', '2015-46', '2015-46', '2015-42', '2015-42'],
'receipts_qty': [3, 2, 3, 4, 2, 3, 3, 4],
'qty': [3.0, 2.0, 5.0, 4.0, 2.0, 3.0, 4.0, 5.0],
'profit': [13.27, 40.94, 292.245, 55.53, 9.9344, 62.11, 190.005, 72.13],
'name': ['milk', 'bread', 'solt', 'cackes', 'milk', 'bread', 'solt', 'cackes'],
'turnover': [17.73, 40.94, 291.36, 92.43, 12.44, 62.11, 210.76, 120.27]})
products[['qty_change','profit_change']] = products.groupby('name')[['qty','profit']].diff()
increase = products[products.qty_change > 0][['name','qty_change','profit_change']]
print(increase)
# name qty_change profit_change
#5 bread 1.0 21.17
#7 cackes 1.0 16.60
decrease = products[products.qty_change < 0][['name','qty_change','profit_change']]
print(decrease)
# name qty_change profit_change
#4 milk -1.0 -3.3356
#6 solt -1.0 -102.2400
我希望这会有所帮助。