This is my dataframe:
import pandas as pd
df = pd.DataFrame(
{
'a': [0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
'b': [0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0]
}
)
And this is the way that I want to group it:
2 1 1
3 0 1
4 0 1
5 0 1
6 0 0
7 0 0
9 1 0
10 0 1
13 1 1
14 0 1
15 0 1
16 0 0
17 0 1
I want to group this dataframe based on values of column b. The first thing to do is to find the 1s in column a. And then I want to continue as long as there is 0 in column b and after that get the row after that 0 as well. If the value in a is 1 and the value in b is 0 I want to continue only for one row. Basically I want to stop as soon as there is a 0 in column b and then go on one row after that 0.
I have tried these two posts: post1, post2 but still have problem to solve this.
I have tried to group them by: df.b.diff().cumsum() but it doesn't give me what I want