2 回答
TA贡献1995条经验 获得超2个赞
创建助手Series通过比较连续值的唯一的组ne(!=)的shiftED值随cumsum。然后groupby用transform和size。仅用于0with 的最后一个 fiter 值numpy.where:
g = df['measure'].ne(df['measure'].shift()).cumsum()
counts = df.groupby(['A','B', g])['measure'].transform('size')
df['Consec_zero_count'] = np.where(df['measure'].eq(0), counts, 0)
print (df)
A B DATE hour measure Consec_zero_count
0 A10 1 1/1/2014 0 0 4
1 A10 1 1/1/2014 1 0 4
2 A10 1 1/1/2014 2 0 4
3 A10 1 1/1/2014 3 0 4
4 A10 2 1/1/2014 4 0 1
5 A10 2 1/1/2014 5 1 0
6 A10 2 1/1/2014 6 2 0
7 A10 3 1/1/2014 7 0 1
8 A11 1 1/1/2014 8 0 2
9 A11 1 1/1/2014 9 0 2
10 A11 1 1/1/2014 10 2 0
11 A11 1 1/1/2014 11 0 2
12 A11 1 1/1/2014 12 0 2
13 A12 2 1/1/2014 13 1 0
14 A12 2 1/1/2014 14 3 0
15 A12 2 1/1/2014 15 0 1
16 A12 4 1/1/2014 16 5 0
17 A12 4 1/1/2014 17 0 1
18 A12 6 1/1/2014 18 0 1
添加回答
举报