I have sports data, exemplified by a running group with distance values associated to date of run and runner's name as per:
import pandas as pd
df=pd.DataFrame({'name': 'Jack Jill Bob Bella Norm Nella Jack Jill Bob Bella Norm Nella Jack Jill Bob Bella Norm Nella'.split(),
'date': '05-04-2021 05-04-2021 05-04-2021 06-04-2021 05-04-2021 06-04-2021 06-04-2021 08-04-2021 11-04-2021 08-04-2021 11-04-2021 08-04-2021 11-04-2021 11-04-2021 15-04-2022 15-04-2022 18-04-2022 19-04-2022'.split(),
'km': [5.85, 5.18, 13.58, 14.45, 14.58, 11.14, 8.85, 10.77, 12.54, 7.09, 7.69, 11.64, 9.82, 11.20, 10.33, 11.31, 14.66, 12.56]})
df['date']=pd.to_datetime(df['date'], infer_datetime_format=True)
I would like to groupby and filter date to provide a rolling, enlarging slice of data to aggregate on. I can do this using a loop and filtering on each unique date, which provides a series of summed km values with unique date subsequently added in as a separate column. The type of data and format I'm after is provided by this code.
for d in df.date.unique():
rolling=df[df.date <= d].groupby('name').sum()
rolling['date']=d
I would like to accomplish using .groupby(), as I have much more data and complexity in what I actually want to do. Happy to be guided to a pre-existing answer that I haven't found after searching...