Efficient pandas rolling aggregation over date range by group - Python 2.7 Windows - Pandas 0.19.2

前端 未结 1 632
借酒劲吻你
借酒劲吻你 2021-01-31 20:45

I\'m trying to find an efficient way to generate rolling counts or sums in pandas given a grouping and a date range. Eventually, I want to be able to add conditions, ie. evaluat

相关标签:
1条回答
  • 2021-01-31 21:29

    Pandas has support for time-aware rolling via the rolling method, so you can use that instead of writing your own solution from scratch:

    def get_rolling_amount(grp, freq):
        return grp.rolling(freq, on='date')['amount'].sum()
    
    df['rolling_sales_180'] = df.groupby('name', as_index=False, group_keys=False) \
                                .apply(get_rolling_amount, '180D')
    

    The resulting output:

        name       date  amount  rolling_sales_180
    0  David 2015-01-01     100              100.0
    1  David 2015-01-05     500              600.0
    2  David 2015-05-30      50              650.0
    3  David 2015-07-25      50              100.0
    4   Ryan 2014-01-04     100              100.0
    5   Ryan 2015-01-19     500              500.0
    6   Ryan 2016-03-31      50               50.0
    7    Joe 2015-07-01     100              100.0
    8    Joe 2015-09-09     500              600.0
    9    Joe 2015-10-15      50              650.0
    
    0 讨论(0)
提交回复
热议问题