首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在熊猫数据中,如何将一批大于指定阈值的1 (ON)标志切换到0?

问在熊猫数据中,如何将一批大于指定阈值的1 (ON)标志切换到0?
EN

Stack Overflow用户
提问于 2019-10-11 10:57:46
回答 1查看 23关注 0票数 1

熊猫数据栏中的国旗列由1或0填充。

问题是如何识别连续的1。

假设t是所需的两类转换的天数阈值:(1)如果在一起的时间超过t1s,则将(t+1)th值从1转到0),如果超过t1s,则将所有1s转换为0。

我的方法是创建两个名为result1和result2的列,并使用这些列进行筛选:

请看这里的图片

我没有想到任何这样的东西,所以没有发布任何代码。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-11 11:34:13

使用:

代码语言:javascript
复制
#compare 0 values
m = df['Value'].eq(0)
#get cumulative sum and filter only 1 rows
g = m.cumsum()[~m]

#set by condition - 0 or ccounter per groups
df['Result1'] = np.where(m, 0, df.groupby(g).cumcount().add(1))
#get maximum per groups with transform for new Series
df['Result2'] = np.where(m, 0, df.groupby(g)['Result1'].transform('max')).astype(int)
代码语言:javascript
复制
print (df)
    Value  Result1  Result2
0       1        1        1
1       0        0        0
2       0        0        0
3       1        1        2
4       1        2        2
5       0        0        0
6       1        1        4
7       1        2        4
8       1        3        4
9       1        4        4
10      0        0        0
11      0        0        0
12      1        1        1
13      0        0        0
14      1        1        1
15      0        0        0
16      0        0        0
17      1        1        6
18      1        2        6
19      1        3        6
20      1        4        6
21      1        5        6
22      1        6        6
23      0        0        0
24      1        1        1
25      0        0        0
26      0        0        0
27      1        1        1
28      0        0        0
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58339797

复制
相关文章

相似问题

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