Skip to content

Instantly share code, notes, and snippets.

@justinhchae
Last active January 12, 2021 02:59
Show Gist options
  • Save justinhchae/554e6be50c35bb9f24b82e6ff545384c to your computer and use it in GitHub Desktop.
Save justinhchae/554e6be50c35bb9f24b82e6ff545384c to your computer and use it in GitHub Desktop.
freq='M'
# or 'D' or 'Y'
df = df[['dates', 'types']].groupby([pd.Grouper(key='dates', freq=freq)]).agg('count').reset_index()
"""
dates count
2 2012-07-31 0
1 2012-06-30 1
3 2012-08-31 1
0 2012-05-31 2
"""
# group by the category being counted, or count in this case
group = df.groupby('count')
print(group)
"""
<pandas.core.groupby.generic.DataFrameGroupBy object at 0x7fc04f3b9cd0>
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment