user2841051
user2841051

Reputation: 13

Is there any way to extract chunks in pandas dataframe based on flag condition occuring multiple times

I have a pandas dataframe as given below

df = pd.DataFrame({"flag":[0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0],
                  "Val":[1, 5, 7, 5, 4, 7, 8, 11, 13, 44, 7 , 8, 10, 14, 12]})

Output:

enter image description here

Output:

I am looking for a way to extract chunks of dataframe based on flag = 1 condition occurring multiple times

Chunk 1
Val flag
7    1
5    1
4    1
7    1

Chunk 2
Val flag
44   1
7    1
8    1
10   1

and so on Any help would be highly appreciated

Upvotes: 1

Views: 315

Answers (1)

jezrael
jezrael

Reputation: 862691

Create Series by compare by Series.ne for not equal with Series.shift and cumulative sum by Series.cumsum for helper groups, then count them for 2 or more rows by Series.map with Series.value_counts filtered by Series.gt, filter only 1 values and chin both condition by & for bitwise AND and pass to boolean indexing, last loop by groups:

g = df['flag'].ne(df['flag'].shift()).cumsum()

m1 = g.map(g.value_counts()).gt(1)
m2 = df['flag'].eq(1)

for k, gr in df[m1 & m2].groupby(g):
    print (gr)

   flag  Val
2     1    7
3     1    5
4     1    4
5     1    7
    flag  Val
9      1   44
10     1    7
11     1    8
12     1   10

Upvotes: 1

Related Questions