Search code examples
pythonpandasnumpypandas-groupbycumsum

Pandas Grouper "Cumulative" sum()


I'm trying to calculate the cumulative total for the next 4 weeks.

Here is an example of my data frame

d = {'account': [10, 10, 10, 10, 10, 10, 10, 10],
     'volume': [25, 60, 40, 100, 50, 100, 40, 50]}
df = pd.DataFrame(d)
df['week_starting'] = pd.date_range('05/02/2021',
                                    periods=8,
                                    freq='W')
df['volume_next_4_weeks'] = [225, 250, 290, 290, 240, 190, 90, 50]
df['volume_next_4_weeks_cumulative'] = ['(25+60+40+100)', '(60+40+100+50)', '(40+100+50+100)', '(100+50+100+40)', '(50+100+40+50)', '(100+40+50)', '(40+50)', '(50)']
df.head(10)

dataframe_table_view

I would to find a way to calculate the cumulative amount by pd.Grouper freq = 4W.


Solution

  • This should work:

    df['volume_next_4_weeks']  = [sum(df['volume'][i:i+4]) for i in range(len(df))]
    

    For the other column showing the addition as string, I have stored the values in a list using the same logic above but not applying sum and then joining the list elements as string:

    df['volume_next_4_weeks_cumulative'] = [df['volume'][i:i+4].to_list() for i in range(len(df))]
    df['volume_next_4_weeks_cumulative'] = df['volume_next_4_weeks_cumulative'].apply(lambda row: ' + '.join(str(x) for x in row))
    

    Now as you mentioned you have different multiple accounts and you want to do it separately for all of them, create a custom function and then use groupby and apply to create the columns:

    def create_mov_cols(df):
        df['volume_next_4_weeks']  = [sum(df['volume'][i:i+4]) for i in range(len(df))]
        df['volume_next_4_weeks_cumulative'] = [df['volume'][i:i+4].to_list() for i in range(len(df))]
        df['volume_next_4_weeks_cumulative'] = df['volume_next_4_weeks_cumulative'].apply(lambda row: ' + '.join(str(x) for x in row))
        return df
    

    Apply the function to the DataFrame:

    df = df.groupby(['account']).apply(create_mov_cols)