简体   繁体   中英

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

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM