Pandas 使用groupby统计值的实例数

Pandas 使用groupby统计值的实例数,pandas,lambda,group-by,apply,Pandas,Lambda,Group By,Apply,我有以下带有id和pid的timeseries数据帧。我想了解每个pid的暂停次数(id 0)和移动次数(id非0) 这里,我认为最小ID至少为3( ID=0>=3 < /代码>) 我期待上述数据的以下输出 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

我有以下带有
id
pid的timeseries数据帧。我想了解每个
pid
的暂停次数(
id 0
)和移动次数(
id非0

这里,我认为最小ID至少为3(<代码> ID=0>=3 < /代码>)

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

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

你想要什么还不清楚。你得到的是
True
False
,因为你的lambda计算一个逻辑运算符(
=
),并且逻辑运算符存在以返回
True
False
。也许你在寻找
df.groupby(['pid',id']).size()
@sshr不清楚你是如何计算的。您是否将连续3次的数据包计算为+1?具体一点。。仅当数据包数超过3行时,才对0个数据包进行计数。在上面的示例中,我们可以忽略最后一行。
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