Skip to content Skip to sidebar Skip to footer

Rolling Sum By Group

Consider this simple example df = pd.DataFrame({'date' : [pd.to_datetime('2018-01-01'), pd.to_datetime('2018-01-01'), pd

Solution 1:

The groupby is adding an index level that is getting in your way.

rs = df.groupby('group').value.rolling(2).sum()
df.assign(rolling_sum=rs.reset_index(level=0, drop=True))

        dategroupvalue  value_useless  rolling_sum
02018-01-01     a      12          NaN
12018-01-01     a      223.022018-01-01     b      32          NaN
32018-01-01     b      427.0

details

rs

# Annoying Index Level# |# v# group   # a      0    NaN#        1    3.0# b      2    NaN#        3    7.0# Name: value, dtype: float64

Alternatively, you can get around the added index by using pd.concat

df.assign(rolling_sum=pd.concat(s.rolling(2).sum() for _, s in df.groupby('group').value))

        dategroupvaluevalue_uselessrolling_sum02018-01-01a12NaN12018-01-01a223.022018-01-01b32NaN32018-01-01b427.0

Post a Comment for "Rolling Sum By Group"