I have data that represent expenses, with date and category, and I want to plot the rolling average per category over time
Sources
I have tried using and combining the following without success
- Python Pandas: Calculate moving average within group doesn't handle dates
- Calculating monthly aggregate of expenses with pandas doesn't handle rolling average
The best I've come with, using the second link is this
import pandas as pd from matplotlib import pyplot as plt df = pd.DataFrame({ "date": ["2023-10-01", "2023-10-20", "2023-12-20", "2023-10-12", "2023-12-12", "2023-12-12", ], "category": ["food", "food", "food", "wear", "wear", "food"], "value": [150, 100, 10, 100, 200, 200], }) df['date'] = pd.to_datetime(df['date']) df.set_index("date", inplace=True) all_s = [] for x in set(df["category"]): s = df.loc[df['category'] == x, "value"] s = s.groupby(pd.Grouper(freq="ME")).sum() all_s.append(s.rename(x)) df = pd.concat(all_s, axis=1).fillna(0).asfreq("ME", fill_value=0) df.plot(style='.-', figsize=(15, 20)) plt.show() Rendering in

Expectation
I expect to add something like a .rolling(window=3, min_periods=1) somewhere, to get kind of a flat line on the graph, to avoid peaks and just have an average over a given period
Источник: https://stackoverflow.com/questions/780 ... r-category