How to group and count rows by month and year using Pandas?

后端 未结 5 653
情歌与酒
情歌与酒 2020-12-13 20:33

I have a dataset with personal data such as name, height, weight and date of birth. I would build a graph with the number of people born in a particular month and year. I\'m

5条回答
  •  悲&欢浪女
    2020-12-13 21:00

    Replace date and count fields with your respective column names. This piece of code will group, sum and sort based on the given parameters. You can also change the frequency to 1M or 2M and so on...

    df[['date', 'count']].groupby(pd.Grouper(key='date', freq='1M')).sum().sort_values(by='date', ascending=True)['count']
    

提交回复
热议问题