我有一个这样的df:
Count
1
0
1
1
0
0
1
1
1
0
我想回来一个 1
如果连续出现两次或多次,则在新列中 1
在 Count
和a 0
如果没有。所以在新专栏中每一行都会得到一个 1
基于该标准在列中得到满足 Count
。那么我想要的输出是:
Count New_Value
1 0
0 0
1 1
1 1
0 0
0 0
1 1
1 1
1 1
0 0
我想我可能需要使用 itertools
但我一直在阅读它,并没有遇到我需要的东西。我希望能够使用此方法计算任意数量的连续出现次数,而不仅仅是2次。例如,有时我需要连续计算10次,我在这里只使用2。
你可以:
df['consecutive'] = df.Count.groupby((df.Count != df.Count.shift()).cumsum()).transform('size') * df.Count
要得到:
Count consecutive
0 1 1
1 0 0
2 1 2
3 1 2
4 0 0
5 0 0
6 1 3
7 1 3
8 1 3
9 0 0
从这里你可以,任何门槛:
threshold = 2
df['consecutive'] = (df.consecutive > threshold).astype(int)
要得到:
Count consecutive
0 1 0
1 0 0
2 1 1
3 1 1
4 0 0
5 0 0
6 1 1
7 1 1
8 1 1
9 0 0
或者,只需一步:
(df.Count.groupby((df.Count != df.Count.shift()).cumsum()).transform('size') * df.Count >= threshold).astype(int)
在效率方面,使用 pandas
当问题的大小增加时,方法提供了显着的加速:
df = pd.concat([df for _ in range(1000)])
%timeit (df.Count.groupby((df.Count != df.Count.shift()).cumsum()).transform('size') * df.Count >= threshold).astype(int)
1000 loops, best of 3: 1.47 ms per loop
相比:
%%timeit
l = []
for k, g in groupby(df.Count):
size = sum(1 for _ in g)
if k == 1 and size >= 2:
l = l + [1]*size
else:
l = l + [0]*size
pd.Series(l)
10 loops, best of 3: 76.7 ms per loop
不确定这是否已经过优化,但您可以尝试一下:
from itertools import groupby
import pandas as pd
l = []
for k, g in groupby(df.Count):
size = sum(1 for _ in g)
if k == 1 and size >= 2:
l = l + [1]*size
else:
l = l + [0]*size
df['new_Value'] = pd.Series(l)
df
Count new_Value
0 1 0
1 0 0
2 1 1
3 1 1
4 0 0
5 0 0
6 1 1
7 1 1
8 1 1
9 0 0