I have a dataframe with a column ID and a binary column, like the example below
ID BINARY_MASK
0 101 1
1 101 0
2 101 1
3 101 1
4 101 1
5 101 1
6 101 0
7 101 1
8 102 1
9 102 1
11 102 1
12 102 1
13 102 0
14 102 0
What I want to do is get the first four consecutive entries that are 1, per ID group. The result I would like to see is the following:
ID BINARY_MASK
2 101 1
3 101 1
4 101 1
5 101 1
8 102 1
9 102 1
11 102 1
12 102 1
The index inside the group where there are four consecutive ones differs per group, like in the example. How do I do this?
I have tried the solution that was offered by Bill G in this question, but that didn't work for me.
Working with Pandas dataframes and Python 3.6