简体   繁体   中英

Rolling sum of groups by period

I have got this dataframe:

lst=[['01012021','A',10],['01012021','B',20],['02012021','A',12],['02012021','B',23]]
df2=pd.DataFrame(lst,columns=['Date','FN','AuM'])

I would like to get the rolling sum by date and FN. The desired result looks like this:

lst=[['01012021','A',10,''],['01012021','B',20,''],['02012021','A',12,22],['02012021','B',23,33]]
df2=pd.DataFrame(lst,columns=['Date','FN','AuM','Roll2PeriodSum'])

Would you please help me?

Thank you

Solution if consecutive datetimes, not used column date for count per groups:

df2['Roll2PeriodSum'] = (df2.groupby('FN').AuM
                            .rolling(2)
                            .sum() 
                            .reset_index(level=0, drop=True))
print (df2)
       Date FN  AuM  Roll2PeriodSum
0  01012021  A   10             NaN
1  01012021  B   20             NaN
2  02012021  A   12            22.0
3  02012021  B   23            43.0

Solution with datetimes, is used column date for counts:

df2['Date'] = pd.to_datetime(df2['Date'], format='%d%m%Y')

df = (df2.join(df2.set_index('Date')
                  .groupby('FN').AuM
                  .rolling('2D')
                  .sum().rename('Roll2PeriodSum'), on=['FN','Date']))
print (df)
        Date FN  AuM  Roll2PeriodSum
0 2021-01-01  A   10            10.0
1 2021-01-01  B   20            20.0
2 2021-01-02  A   12            22.0
3 2021-01-02  B   23            43.0

df = (df2.join(df2.set_index('Date')
                  .groupby('FN').AuM
                  .rolling('2D', min_periods=2)
                  .sum()
                  .rename('Roll2PeriodSum'), on=['FN','Date']))
print (df)
        Date FN  AuM  Roll2PeriodSum
0 2021-01-01  A   10             NaN
1 2021-01-01  B   20             NaN
2 2021-01-02  A   12            22.0
3 2021-01-02  B   23            43.0

Use groupby.rolling.sum :

df2['Roll2PeriodSum'] = (
    df2.assign(Date=pd.to_datetime(df2['Date'], format='%d%m%Y'))
       .groupby('FN').rolling(2)['AuM'].sum().droplevel(0)
)
print(df2)

# Output
       Date FN  AuM  Roll2PeriodSum
0  01012021  A   10             NaN
1  01012021  B   20             NaN
2  02012021  A   12            22.0
3  02012021  B   23            43.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM