Skip to content

Instantly share code, notes, and snippets.

@justinhchae
Last active January 12, 2021 15:11
Show Gist options
  • Select an option

  • Save justinhchae/5c14f7bd21b26a0993f9e5c2a19e27cf to your computer and use it in GitHub Desktop.

Select an option

Save justinhchae/5c14f7bd21b26a0993f9e5c2a19e27cf to your computer and use it in GitHub Desktop.
gitcsv = 'https://raw.githubusercontent.com/justinhchae/medium/main/sample.csv'
df = pd.read_csv(gitcsv, index_col=0)
df['dates'] = pd.to_datetime(df['dates'])
freq='M'
df = df.groupby(['types', pd.Grouper(key='dates', freq=freq)])['types'].agg(['count']).reset_index()
print(df)
"""
types dates count
0 b 2016-01-31 1
1 a 2016-01-31 5
2 b 2016-02-29 3
3 a 2016-02-29 4
4 b 2016-03-31 3
5 a 2016-03-31 6
6 b 2016-04-30 1
...
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment