I have a dataframe that looks like this:
stuff datetime value
A 1/1/2019 3
A 1/2/2019 4
A 1/3/2019 5
A 1/4/2019 6
...
I want to create a new dataframe that looks like this:
stuff avg_3 avg_4 avg_5
A 3.4 4.5 5.5
B 2.3 4.2 6.1
where avg_3 is the avg of the last 3 days from today, avg_4 is the avg of the last 4 days from today etc grouped by stuff
How do I do that?
My current code:
df.groupby('stuff').apply(lambda x: pd.Series(dict(
day_3=(x.datetime > datetime.now() - timedelta(days = 3)).mean(),
day_7=(x.datetime > datetime.now() -timedelta(days = 7)).mean())))
Thanks in advance