GroupBandas将连续计数归零

编程入门 行业动态 更新时间:2024-10-18 10:34:59
本文介绍了GroupBandas将连续计数归零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的输入看起来像下面的df.

My input looks like the below df.

我需要按列(A,B)分组并计算连续零的数量/计算每个组中连续零的长度,然后写入新列"Zero_count"

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

我尝试使用"groupby"技术来获取组,但是我一直在寻找组内连续的零计数.我尝试使用lambda函数,但是它计算的是零的总数,而我有兴趣重复连续的零.我希望我的输出看起来像这样:

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!

推荐答案

通过 ne (!=) /stable/generated/pandas.Series.shift.html"rel =" nofollow noreferrer> shift ed值和 cumsum .然后 groupby 与 transform 和 size .仅对0具有 numpy.where

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

更多推荐

GroupBandas将连续计数归零

本文发布于:2023-11-13 02:13:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583132.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:GroupBandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!