熊猫groupby自定义聚合计算

时间:2020-10-08 16:52:31

标签: python pandas dataframe group-by aggregate

我有以下数据框,其中country wise orders data最近2周。我想计算出与上周相比,按国家,开始日期和结束日期分组的食物订单与上周相比变化(增加或减少)的百分比

enter image description here

Formula = (this_week_orders*100)/(last_week_orders) - 100

对于上面的df,我想获得如下的最终数据帧

我们可以看到,与上周相比Russia had a decrease in order count of 33%India had an increase of 50%

enter image description here

请建议如何编写自定义聚合函数,因为我只熟悉标准的sum(),count()等...

1 个答案:

答案 0 :(得分:1)

df = pd.DataFrame({'start_date':['2020-09-21','2020-09-21','2020-09-28', '2020-09-28'], 
                   'end_date':['2020-09-27', '2020-09-27', '2020-10-04', '2020-10-04'],
                  'Country':['Russia', 'India','Russia','India'], 
                   'orders':[150,80,100,120]})
df['start_date'] = pd.to_datetime(df['start_date'])

df.sort_values(by='start_date', inplace=True)

df['% Change'] = df.groupby('Country')['orders'].pct_change()

输出

    start_date    end_date  Country orders  % Change
0   2020-09-21  2020-09-27  Russia  150          NaN
1   2020-09-21  2020-09-27  India   80           NaN
2   2020-09-28  2020-10-04  Russia  100    -0.333333
3   2020-09-28  2020-10-04  India   120     0.500000