首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对标记的特定块进行计数

对标记的特定块进行计数
EN

Stack Overflow用户
提问于 2018-03-05 19:45:52
回答 2查看 49关注 0票数 1

我想根据LabelID属性计算数据帧中"1“块的数量。例如,给定以下数据帧:

DF输入:

代码语言:javascript
复制
   eventTime                 velocity     LabelId
1  2017-08-19 12:53:55.050         3        0
2  2017-08-19 12:53:55.100         4        1
3  2017-08-19 12:53:55.150       180        1
4  2017-08-19 12:53:55.200         2        1
5  2017-08-19 12:53:55.250         5        0
6  2017-08-19 12:53:55.050         3        0
7  2017-08-19 12:53:55.100         4        1
8  2017-08-19 12:53:55.150        70        1
9  2017-08-19 12:53:55.200         2        1
10 2017-08-19 12:53:55.250         5        0

Output=2,因为它有两个块1. Block_1=rows 2-4Block_2=rows 7-9。请提供任何帮助,我们将非常感激。

致以最好的问候,卡洛

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-05 19:53:08

我们可以使用diff()。如下所示:

代码语言:javascript
复制
d = df.LabelId.diff()
d.iloc[0] = df.LabelId.iloc[0]

这为您提供了:

代码语言:javascript
复制
[0, 1, 0, 0, -1, 0, 1, 0, 0, -1]

1组的数量是diff为1的次数。因此:

代码语言:javascript
复制
(d == 1).sum()

给你答案。

票数 2
EN

Stack Overflow用户

发布于 2018-03-05 19:59:24

下面是另一种简单的方法:

代码语言:javascript
复制
INTERESTING_LABEL = 1
df = ...  # Make data frame
# Find positions where the label is not present
s = (df.LabelId != INTERESTING_LABEL)
# Counter that increases where the label is not present
# Then select where the label is present and count unique values
num_blocks = s.cumsum()[~s].nunique()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49109770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档