suppose there are record
index | order | group | Data |
---|---|---|---|
1 | 1 | A | 0 |
2 | 2 | A | 0 |
3 | 3 | A | 0 |
4 | 4 | A | 1 |
5 | 5 | A | 2 |
6 | 6 | A | 1 |
7 | 1 | B | 0 |
8 | 2 | B | 2 |
9 | 3 | B | -1 |
10 | 4 | B | 0 |
11 | 5 | B | 2 |
12 | 6 | B | 4 |
I would like to exclude leading 0 record Data in each group
Expecting result
index | order | group | Data |
---|---|---|---|
4 | 4 | A | 1 |
5 | 5 | A | 2 |
6 | 6 | A | 1 |
8 | 2 | B | 2 |
9 | 3 | B | -1 |
10 | 4 | B | 0 |
11 | 5 | B | 2 |
12 | 6 | B | 4 |
how do I approached for this? I saw lstrip() but it only applied in each record.
CodePudding user response:
Use GroupBy.cummax
with comapre for not equal 0
in boolean indexing
:
df1 = df[df['Data'].ne(0).groupby(df['group']).cummax().ne(0)]
print (df1)
index order group Data
3 4 4 A 1
4 5 5 A 2
5 6 6 A 1
7 8 2 B 2
8 9 3 B -1
9 10 4 B 0
10 11 5 B 2
11 12 6 B 4