获取Pandas中按索引分组的各个值的计数

时间:2018-04-26 09:48:42

标签: pandas dataframe pivot

我有一个Pandas Dataframe如下:

                        User_ID                         Title                          Activity
9fbb7702-5209-46c8-b7c8-2c3d03550b56                On the Gold                    VIEWED_MOVIE
9fbb7702-5209-46c8-b7c8-2c3d03550b56   The Amazing Spider-Man 2                   VIEWED_TVSHOW
9fbb7702-5209-46c8-b7c8-2c3d03550b56               Pearl Harbor                    VIEWED_MOVIE
9fbb7702-5209-46c8-b7c8-2c3d03550b56            Big Top Pee-wee                    VIEWED_MOVIE
9fbb7702-5209-46c8-b7c8-2c3d03550b56              The Virginian                    VIEWED_MOVIE
9fbb7702-5209-46c8-b7c8-2c3d03550b56     Lies My Mother Told Me                    VIEWED_MOVIE
9fbb7702-5209-46c8-b7c8-2c3d03550b56        My Fellow Americans                    VIEWED_MOVIE
9fbb7702-5209-46c8-b7c8-2c3d03550b56        My Fellow Americans                    VIEWED_MOVIE
9fbb7702-5209-46c8-b7c8-2c3d03550b56                  Yogi Bear     MARKED_CONTENT_AS_FAVOURITE
9fbb7702-5209-46c8-b7c8-2c3d03550b56     The Quick and the Dead                    VIEWED_MOVIE
9fbb7702-5209-46c8-b7c8-2c3d03550b56                    My Girl                    VIEWED_MOVIE
9fbb7702-5209-46c8-b7c8-2c3d03550b56                  My Girl 2                    VIEWED_MOVIE
9fbb7702-5209-46c8-b7c8-2c3d03550b56                       Taxi                    VIEWED_MOVIE
9fbb7702-5209-46c8-b7c8-2c3d03550b56                Monte Walsh                    VIEWED_MOVIE
9fbb7702-5209-46c8-b7c8-2c3d03550b56            The Love Letter                    VIEWED_MOVIE
9fbb7702-5209-46c8-b7c8-2c3d03550b56                 Rio Diablo                    VIEWED_MOVIE
9fbb7702-5209-46c8-b7c8-2c3d03550b56     Magic in the Moonlight                    VIEWED_MOVIE
9fbb7702-5209-46c8-b7c8-2c3d03550b56             The Right Kite                    VIEWED_MOVIE
9fbb7702-5209-46c8-b7c8-2c3d03550b56             Worlds Collide                   VIEWED_TVSHOW
9fbb7702-5209-46c8-b7c8-2c3d03550b56                 Big Driver                   VIEWED_TVSHOW
9fbb7702-5209-46c8-b7c8-2c3d03550b56                 Big Driver                    VIEWED_MOVIE
cb1fc554-8566-4c9f-a3ca-f64be302d65e                       null     MARKED_CONTENT_AS_FAVOURITE
cb1fc554-8566-4c9f-a3ca-f64be302d65e                       NULL                VIEWED_CELEBRITY
6916484f-b7bd-431a-818d-d1a63ff7c717                       NULL                          SEARCH
6916484f-b7bd-431a-818d-d1a63ff7c717                   Spy Game                          SEARCH
6916484f-b7bd-431a-818d-d1a63ff7c717               Act of Valor                    VIEWED_MOVIE
6916484f-b7bd-431a-818d-d1a63ff7c717  One Direction: This Is Us                    VIEWED_MOVIE
6916484f-b7bd-431a-818d-d1a63ff7c717   The Amazing Spider-Man 2                    VIEWED_MOVIE
9fbb7702-5209-46c8-b7c8-2c3d03550b56                       NULL                    VIEWED_MOVIE
9fbb7702-5209-46c8-b7c8-2c3d03550b56                   Spy Game                          SEARCH
9fbb7702-5209-46c8-b7c8-2c3d03550b56            Little Man Tate                    VIEWED_MOVIE

我想了解每位用户的每项活动,如下所示。

                               User_Id MARKED_CONTENT_AS_FAVOURITE RATE_CONTENT SEARCH VIEWED_CELEBRITY VIEWED_MOVIE VIEWED_TVSHOW
1 6916484f-b7bd-431a-818d-d1a63ff7c717                           0            0      1                0            4             0
2 9fbb7702-5209-46c8-b7c8-2c3d03550b56                           2            2      1                1           20             3
3 cb1fc554-8566-4c9f-a3ca-f64be302d65e                           0            0      1                1            0             0 

我已经使用过:

distinct_activity.groupby(['User_Id','Activity']).agg({"Activity": "count"})

但我没有按照需要获得输出。

0 个答案:

没有答案