熊猫数据栏中的国旗列由1或0填充。
问题是如何识别连续的1。
假设t是所需的两类转换的天数阈值:(1)如果在一起的时间超过t1s,则将(t+1)th值从1转到0),如果超过t1s,则将所有1s转换为0。
我的方法是创建两个名为result1和result2的列,并使用这些列进行筛选:
我没有想到任何这样的东西,所以没有发布任何代码。
发布于 2019-10-11 11:34:13
使用:
#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)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 0https://stackoverflow.com/questions/58339797
复制相似问题