Skip to content Skip to sidebar Skip to footer

Indentify Consecutive Cells By A Condition Value

I would like to know how to make an extra column on the below dataframe that will be 1 when on the age column are 3 or more consecutive values bigger than 35 Data age 0 12 1 50

Solution 1:

First compare values by Series.gt for >, then create consecutive groups by shift with cumsum, last grouping by groups and get counts with GroupBy.transform - compare by Series.ge and chain with original s for prevent set 3 consecutive >35 values, last set values to integers for True/False to 1/0 mapping:

s = df['age'].gt(35)
g = s.ne(s.shift()).cumsum()

df['flag'] = (s.groupby(g).transform('size').ge(3) & s).astype(int)
print (df)
   age  flag
0   12     0
1   50     0
2   49     0
3   29     0
4   55     0
5   34     0
6   23     0
7   46     1
8   87     1
9   39     1

Post a Comment for "Indentify Consecutive Cells By A Condition Value"