我有一个布尔型的pandas DataFrame
:
w=pd.DataFrame(data=[True,False,True,True,True,False,False,True,False,True,True,False,True])
我正在尝试识别长度至少为N的True
值块:
我可以按照SO上的建议进行操作:
N=3.0
b = w.ne(w.shift()).cumsum() *w
m = b[0].map(b[0].mask(b[0] == 0).value_counts()) >= N
这个功能很好用,返回结果正常
m
0 False
1 False
2 True
3 True
4 True
5 False
6 False
7 False
8 False
9 False
10 False
11 False
12 False
现在,我需要做同样的事情,但需要允许一些容差来确定块。因此,我想识别至少有N个元素的所有块,但允许M个值(可以任意放置在块内)为 False
。
以示例w为例,当N=3,M=1时应为:
w
0 True
1 False
2 True
3 True
4 True
5 False
6 False
7 True
8 False
9 True
10 True
11 False
12 True
与以前的结果不同,位置在:
desidered=
0 **True**
1 **True**
2 True
3 True
4 True
5 False
6 False
7 True
8 ** True **
9 True
10 True
11 **True**
12 True