Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
684 views
in Technique[技术] by (71.8m points)

python - GroupBy Pandas Count Consecutive Zero's

My input looks like the below df.

I need to group by column (A, B) and count the number of consecutive zeros/ count the length of the consecutive zeros in each of the groups and write to a new column "Zero_count"

Input:
A    B  DATE      hour  measure     
A10  1  1/1/2014    0   0       
A10  1  1/1/2014    1   0       
A10  1  1/1/2014    2   0       
A10  1  1/1/2014    3   0       
A10  2  1/1/2014    4   0       
A10  2  1/1/2014    5   1       
A10  2  1/1/2014    6   2       
A10  3  1/1/2014    7   0       
A11  1  1/1/2014    8   0       
A11  1  1/1/2014    9   0       
A11  1  1/1/2014    10  2       
A11  1  1/1/2014    11  0       
A11  1  1/1/2014    12  0       
A12  2  1/1/2014    13  1       
A12  2  1/1/2014    14  3       
A12  2  1/1/2014    15  0       
A12  4  1/1/2014    16  5       
A12  4  1/1/2014    17  0       
A12  6  1/1/2014    18  0       

I tried using "groupby" technique to get the groups, but consecutive zero counting within the group is something that I am looking for. I have tried to use lambda function but that counts the total number of zeros, while I am interested in repeating consecutive zeros. I want my output to look like this:

Output
A    B  DATE      hour  measure Consec_zero_count
A10  1  1/1/2014    0   0       4
A10  1  1/1/2014    1   0       4
A10  1  1/1/2014    2   0       4
A10  1  1/1/2014    3   0       4
A10  2  1/1/2014    4   0       1
A10  2  1/1/2014    5   1       0
A10  2  1/1/2014    6   2       0
A10  3  1/1/2014    7   0       1
A11  1  1/1/2014    8   0       2
A11  1  1/1/2014    9   0       2
A11  1  1/1/2014    10  2       0
A11  1  1/1/2014    11  0       2
A11  1  1/1/2014    12  0       2
A12  2  1/1/2014    13  1       0
A12  2  1/1/2014    14  3       0
A12  2  1/1/2014    15  0       1
A12  4  1/1/2014    16  5       0
A12  4  1/1/2014    17  0       1
A12  6  1/1/2014    18  0       1

Any leads would be appreciated. Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Create helper Series for unique groups of consecutive values by compare by ne (!=) of shifted values with cumsum. Then groupby with transform and size. Last fiter values only for 0 with 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...