see docs:https://pandas.pydata.org/docs/user_guide/window.html
If you want to write a one-liner (perhaps you want to pass the methods into a pipeline), you can do so by first setting as_index
parameter of groupby
method to False to return a dataframe from the aggregation step and use assign()
to assign a new column to it (the cumulative sum for each person).
These chained methods return a new dataframe, so you'll need to assign it to a variable (e.g. agg_df
) to be able to use it later on.
agg_df = (# aggregate df by name and daydf.groupby(['name','day'], as_index=False)['no'].sum().assign(# assign the cumulative sum of each name as a new columncumulative_sum=lambda x: x.groupby('name')['no'].cumsum()))