Stefano Potter Stefano Potter - 5 months ago 26
Python Question

Counting consecutive occurrences of a value

I have a df like so:

Count
1
0
1
1
0
0
1
1
1
0


and I want to return a
1
in a new column if there are two or more consecutive occurrences of
1
in
Count
and a
0
if there is not. So in the new column each row would get a
1
based on this criteria being met in the column
Count
. My desired output would then be:

Count New_Value
1 0
0 0
1 1
1 1
0 0
0 0
1 1
1 1
1 1
0 0


I am thinking I may need to use
itertools
but I have been reading about it and haven't come across what I need yet. I would like to be able to use this method to count any number of consecutive occurrences, not just 2 as well. For example, sometimes I need to count 10 consecutive occurrences, I just use 2 in the example here.

Answer

Not sure if this is optimized, but you can give it a try:

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
Comments