Home > OS >  How do I resample and value_count at the same time in pandas?
How do I resample and value_count at the same time in pandas?

Time:04-17

I have a data frame with fields for type of cat and a timestamp.

I can see all value counts of my field like this:

df["cat_type"].value_counts()
Calico         2870
Tabby           102
Tortoise_shell   51
Name: cat_type, dtype: int64

And I can see all counts by month like this:

df.set_index("my_time")[["cat_type"]].resample("M").count()
       cat_type
my_time     
2012-01-31  936
2012-02-29  1236
2012-03-31  851

However, I don't know how to do /both/ value counts by month.

I'm looking for an output like...

my_time     calico  tabby tortoise_shell
2012-01-31     M1      M2             M3
2012-02-29     N1      N2             N3
2012-03-31     O1      O2             O3

What's that syntax?

It is not

df.set_index("my_time")[["cat_type"]].resample("M").nunique()
       cat_type
my_time     
2012-01-31  3
2012-02-29  3
2012-03-31  3

CodePudding user response:

You can try pd.Grouper

out = df.groupby([pd.Grouper(key = 'my_time',freq='M'),df['cat_type']]).count().unstack()
  • Related