使用groupby pandas计算值的实例数

时间:2016-11-21 21:38:21

标签: pandas lambda group-by apply

我有idpid's的以下时间序列数据框。我有兴趣找出每个id 0的暂停(id not 0)和没有动作(pid)。

这里我认为最小ID至少为3(id==0 >=3

dateTime                          id            pid
5/21/2016 15:23 3:23:25            0             a
5/21/2016 15:23 3:23:26            0             a
5/21/2016 15:23 3:23:27            0             a
5/21/2016 15:23 3:23:28            0             a
5/21/2016 15:23 3:23:29            0             a
5/21/2016 15:23 3:23:30            1             a
5/21/2016 15:23 3:23:31            2             a
5/21/2016 15:23 3:23:32            3             a
5/21/2016 15:23 3:23:33            0             a
5/21/2016 15:23 3:23:34            1             a
5/21/2016 15:23 3:40:25            0             a
5/21/2016 15:23 3:40:26            0             a
5/21/2016 15:23 3:40:27            0             a
5/21/2016 15:23 3:40:28            1             a
5/21/2016 15:23 3:40:29            1             a
5/21/2016 15:23 3:23:35            0             b
5/21/2016 15:23 3:23:36            0             b
5/21/2016 15:30 3:30:02            0             b
5/21/2016 15:30 3:30:03            2             b
5/21/2016 15:30 3:30:04            3             b
5/21/2016 15:30 3:30:05            1             b
5/21/2016 15:30 3:30:06            2             b
5/21/2016 15:30 3:30:07            0             b

我期待上述数据的以下输出

nr_paused   nr_move     pid
 2          2       a
 1          1       b

nr_paused = Occurrences of id '0'  for more than 3 times then each packet should be counted as 1. 
In the above example, there are two such instances (rows 1-5 and 11-13) hence nr_paused for id 'a' is 2
Similarly, for b nr_paused=1 (rows 16-18)
The count nr_move should be id > 0 rows

0 个答案:

没有答案