按数据框

时间:2015-06-26 19:01:19

标签: python pandas

我有一个如下所示的数据框:

+---+-----------+----------------+-------+
|   |    uid    |      msg       | count |
+---+-----------+----------------+-------+
| 0 | 121437681 | eis            |     1 |
| 1 |  14403832 | eis            |     1 |
| 2 | 190442364 | eis            |     1 |
| 3 | 190102625 | eis            |     1 |
| 4 | 190428772 | eis_reply      |     1 |
| 5 | 190428772 | single_message |     1 |
| 6 | 190428772 | yes            |     1 |
| 7 | 190104837 | eis            |     1 |
| 8 | 144969454 | eis            |     1 |
| 9 | 190738403 | eis            |     1 |
+---+-----------+----------------+-------+

我想要做的是为每个uid计算每个msg的实例。

我创建了一个groupby对象并找到了所有消息的计数:

grouped_test = test.groupby('uid')
grouped_test.count('msg') 

但我不太清楚如何为每个uid计算每种类型的消息。我正在考虑创建掩码和4个独立的数据帧,但这似乎并不是一种有效的方法。

示例数据 - http://www.sharecsv.com/s/16573757eb123c5b15cae4edcb7296e3/sample_data.csv

2 个答案:

答案 0 :(得分:11)

按uid分组并将value_counts应用于msg列:

>>> d.groupby('uid').msg.value_counts()
uid                      
14403832   eis               1
121437681  eis               1
144969454  eis               1
190102625  eis               1
190104837  eis               1
190170637  eis               1
190428772  eis               1
           single_message    1
           yes               1
           eis_reply         1
190442364  eis               1
190738403  eis               1
190991478  single_message    1
           eis_reply         1
           yes               1
191356453  eis               1
191619393  eis               1
dtype: int64

答案 1 :(得分:2)

groupbyid上应用msg,然后对每个count求和:

>>> df.groupby(['uid', 'msg'])['count'].sum()
uid        msg           
14403832   eis               1
121437681  eis               1
144969454  eis               1
190102625  eis               1
190104837  eis               1
190170637  eis               1
190428772  eis               1
           eis_reply         1
           single_message    1
           yes               1
190442364  eis               1
190738403  eis               1
190991478  eis_reply         1
           single_message    1
           yes               1
191356453  eis               1
191619393  eis               1
Name: count, dtype: int64

您可以重置索引以检索展平版本:

>>> df.groupby(['uid', 'msg'])['count'].sum().reset_index()
          uid             msg  count
0    14403832             eis      1
1   121437681             eis      1
2   144969454             eis      1
3   190102625             eis      1
4   190104837             eis      1
5   190170637             eis      1
6   190428772             eis      1
7   190428772       eis_reply      1
8   190428772  single_message      1
9   190428772             yes      1
10  190442364             eis      1
11  190738403             eis      1
12  190991478       eis_reply      1
13  190991478  single_message      1
14  190991478             yes      1
15  191356453             eis      1
16  191619393             eis      1