The group_id should be increasing for each binary that is = 1 but grouping all consecutive 0's into one group_id, with the next group of consecutive 0's being in another group_id.
I have been using
df['group_id1'] = df['diff'].cumsum()
and getting the group_id1 column as a result. The result I am looking for is group_id2.
diff group_id1 group_id2
0 1 1 1
1 0 1 2
2 0 1 2
3 0 1 2
4 0 1 2
5 0 1 2
6 0 1 2
7 0 1 2
8 0 1 2
9 1 2 3
10 1 3 4
11 1 4 5
12 1 5 6
13 1 6 7
14 1 7 8
15 1 8 9
16 1 9 10
17 1 10 11
18 0 10 12
19 0 10 12
Assuming you only have 0/1, you can use
diffto identify the 0s preceded by 1s:If you have other values go with
shift:Another variant:
Output: