'Fil rows in df.column betwen rows if number of rows between is less than something

I have a df where i want to fill the rows in column values with True if the number of rows between values True in column values is less then two.

counter values
1 True
2 False
3 False
4 True
5 False
6 True
7 True
8 False
9 True
10 False
11 False

The result i want is like the df below:

counter values
1 True
2 False
3 False
4 True
5 True
6 True
7 True
8 True
9 True
10 False
11 False


Solution 1:[1]

You can make groups starting with True, if the group is 2 items (or less), replace with True. Then compute the boolean OR with the original column:

N = 2
fill = df['values'].groupby(df['values'].cumsum()).transform(lambda g: len(g)<=N)
df['values'] = df['values']|fill  ## or df['values'] |= fill

output (here as new column value2 for clarity):

    counter  values  values2
0         1    True     True
1         2   False    False
2         3   False    False
3         4    True     True
4         5   False     True
5         6    True     True
6         7    True     True
7         8   False     True
8         9    True     True
9        10   False    False
10       11   False    False

Other option that works only in the particular case of N=2, check if both the row before and after is True:

df['values'] = df['values']|(df['values'].shift()&df['values'].shift(-1))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1