假设我有以下数据序列:
Date Category
2014-8 Facebook
2014-8 Vimeo
2014-8 Facebook
2014-8 Facebook
2014-9 Facebook
2014-9 Orkut
2014-9 Facebook
2014-9 Facebook
2014-9 Facebook
...
2014-10 Youtube
2014-10 DailyMotion
2014-10 Facebook
2014-10 Vimeo
2014-10 Facebook
2014-10 Facebook我想对每个类别(时间序列中的唯一价值/因素)每月和每年进行统计。
Category Date Count
Facebook 2014-01 5
2014-02 6
2014-03 8
Vimeo 2014-01 3
2014-02 10
2014-03 9
youtube 2014-01 13
2014-02 61
2014-03 8所以,当我打电话给Facebook时,我可以看到每个月facebook发生了多少次。
我试过的是:
df['Date'] = df['Date'].map(lambda x: '{year}-{month}'.format(year=x.year,
month=x.month,
day=x.day))
a = df.groupby(['Category','year-month']).size()发布于 2015-05-16 23:54:50
您需要同时按类别和日期进行分组,然后在日期上进行计数:
>>> df.groupby(['Category', 'Date']).Date.count()
Category Date
DailyMotion 2014-10 1
Facebook 2014-10 3
2014-8 3
2014-9 4
Orkut 2014-9 1
Vimeo 2014-10 1
2014-8 1
Youtube 2014-10 1
Name: Date, dtype: int64获取某一特定类别的每月总数(例如:(“‘Facebook”),您首先需要过滤类别:
>>> df[df.Category == 'Facebook'].groupby(['Category', 'Date']).Date.count()
Category Date
Facebook 2014-10 3
2014-8 3
2014-9 4
Name: Date, dtype: int6https://stackoverflow.com/questions/30281861
复制相似问题