使用python pandas 按月年份计数发生频率 [英] Counting frequency of occurrence by month-year using python panda

查看:157
本文介绍了使用python pandas 按月年份计数发生频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我具有以下数据系列:

Suppose I have following Data-Series:

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

我想每月和每年对每个类别(时间序列中的唯一值/因数)进行计数.

I would like to make a count of each category (Unique Value/Factor in the Time Series) per month and year.

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出现.

So, when I call Facebook, I can see how many times facebook occured on each month.

我尝试过的是:

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()

预先感谢您的帮助和建议.

Thank you in advance for help and suggestion.

推荐答案

您需要按类别和日期进行分组,然后对日期进行计数:

You need to group by both Category and Date and then do a count on Date:

>>> 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")的每月总计,您首先需要按类别进行过滤:

To get the monthly totals for a specific category (e.g. 'Facebook'), you first need to filter on the category:

>>> df[df.Category == 'Facebook'].groupby(['Category', 'Date']).Date.count()
Category  Date   
Facebook  2014-10    3
          2014-8     3
          2014-9     4
Name: Date, dtype: int6

这篇关于使用python pandas 按月年份计数发生频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆