我有一个类似于下面的数据集:
product_ID month amount_sold
1 1 23
1 2 34
1 3 85
2 1 47
2 2 28
2 3 9
3 1 73
3 2 84
3 3 12
我希望输出如下:
例如,对于产品1:
-avg_monthly_growth
计算公式为((85-34)/34*100+(34-23)/23*100)/2=98.91%
-lowest_monthly_growth
是(34-23)/23*100)=47.83%
-highest_monthly_growth
是(85-34)/34*100)=150%
-current_monthly_growth
是最近两个月之间的增长(在本例中,是从第2个月到第3个月的增长,因为每个产品的month
范围为1-3)
product_ID avg_monthly_growth lowest_monthly_growth highest_monthly_growth current_monthly_growth
1 98.91% 47.83% 150% 150%
2 ... ... ... ...
3 ... ... ... ...
我试过df.loc[df.groupby('product_ID')['amount_sold'].idxmax(), :].reset_index()
得到最大值(同样也得到最小值),但我不太确定如何得到增长百分比。你知道吗
您可以在
axis=1
上使用带有hpct_change()
的pivot_table
,然后用所需序列创建字典并创建df:相关问题 更多 >
编程相关推荐