Q:

How to change the bool values in pd.Series if the count is more than some value in pandas

I have the following bool series, I am trying to find the count, I have to check the False values, if the count of the False values in consecutive series is greater than 2 then let it be false but if the count is less than equal to 2 then i have to inverse them, from False to True 
Expected output: Like wise first time the False is repeating for two times which means it will change into True, but if we see after true values the false is repeating again more than two times so those values will stay as false,
How can I perform this using Pandas functions?
True
True
True
True
True
False
False
True
True
True
True
True
True
True
False
False
False
False
False
False
True
True
True
True

A:

Let us try something different 
s=df.cumsum().mask(df)
df=df.mask(s.isin(s.value_counts()[s.value_counts()<=2].index),True)
df
0      True
1      True
2      True
3      True
4      True
5      True
6      True
7      True
8      True
9      True
10     True
11     True
12     True
13     True
14    False
15    False
16    False
17    False
18    False
19    False
20     True
21     True
22     True
23     True
Name: a, dtype: bool

A:

Try using groupby and cumsum to generate unique groups of False, then get the count of each group, if that count is less than three invert that group of series using ~ and apply back to series with mask:
s.mask(s.groupby((s).cumsum().where(~s)).transform('count') < 3, ~s)

Output:
0      True
1      True
2      True
3      True
4      True
5      True
6      True
7      True
8      True
9      True
10     True
11     True
12     True
13     True
14    False
15    False
16    False
17    False
18    False
19    False
20     True
21     True
22     True
23     True
Name: 0, dtype: bool

