python - How can I bin by date ranges and categories in pandas? -
i have data frame date, category , value. i'd plot sum-aggregated values per category. example want sum values happen in 3 day periods, each category individually.
an attempt seems complicating is
import random import datetime dt import pandas pd random.seed(0) df=pd.dataframe([[dt.datetime(2000,1,random.randint(1,31)), random.choice("abc"), random.randint(1,3)] _ in range(100)], columns=["date", "cat", "value"]) df.set_index("date", inplace=true) result=df.groupby("cat").resample("3d", how="sum").unstack("cat").value.fillna(0) result.plot()
this right logic, resampling doesn't have fixed start, date ranges 3-day periods don't align between categories (and nan/0 values).
what better way achieve plot?
i think should group cat
, date
:
df = pd.dataframe([[dt.datetime(2000,1,random.randint(1,31)), random.choice("abc"), random.randint(1,3)] _ in range(100)], columns=["date", "cat", "value"]) df.groupby(["cat", pd.grouper(freq='3d',key='date')]).sum().unstack(0).fillna(0).plot()
Comments
Post a Comment