Counting frequency of values by date using pandas

后端 未结 3 773
一个人的身影
一个人的身影 2020-12-28 16:00

Let\'s suppose I have following Time Series:

Timestamp              Category
2014-10-16 15:05:17    Facebook
2014-10-16 14:56:37    Vimeo
2014-10-16 14:25:16         


        
3条回答
  •  悲哀的现实
    2020-12-28 16:32

    It might be easiest to turn your Series into a DataFrame and use Pandas' groupby functionality (if you already have a DataFrame then skip straight to adding another column below).

    If your Series is called s, then turn it into a DataFrame like so:

    >>> df = pd.DataFrame({'Timestamp': s.index, 'Category': s.values})
    >>> df
           Category           Timestamp
    0      Facebook 2014-10-16 15:05:17
    1         Vimeo 2014-10-16 14:56:37
    2      Facebook 2014-10-16 14:25:16
    ...
    

    Now add another column for the week and year (one way is to use apply and generate a string of the week/year numbers):

    >>> df['Week/Year'] = df['Timestamp'].apply(lambda x: "%d/%d" % (x.week, x.year))
    >>> df
                 Timestamp     Category Week/Year
    0  2014-10-16 15:05:17     Facebook   42/2014
    1  2014-10-16 14:56:37        Vimeo   42/2014
    2  2014-10-16 14:25:16     Facebook   42/2014
    ...
    

    Finally, group by 'Week/Year' and 'Category' and aggregate with size() to get the counts. For the data in your question this produces the following:

    >>> df.groupby(['Week/Year', 'Category']).size()
    Week/Year  Category   
    41/2014    DailyMotion    1
               Facebook       3
               Vimeo          2
               Youtube        3
    42/2014    Facebook       7
               Orkut          1
               Vimeo          1
    

提交回复
热议问题