Home > Blockchain >  How do I get the count per category aggregated by month using python's pandas?
How do I get the count per category aggregated by month using python's pandas?

Time:10-26

I have a dataset that looks like this (link for sample data is below)

I was hoping to get the count of each 'frame' per type and per month. So it would give this kind of result: I tried it with groupby and value_counts and can only do it with one layer such as origin_type and frame only

CodePudding user response:

Can you try this:

df = pd.read_excel("Sample_Data.xlsx")
df.Publication_Date = pd.to_datetime(df.Publication_Date)
df['yr'] = df.Publication_Date.dt.year
df['mon'] = df.Publication_Date.dt.month
df.groupby(['yr', 'mon', 'Origin_Type', 'frames'])['frames'].count()
  • Related