Issue
I want to get all the users from a dataframe where a specific column goes from 1
to 0
.
For example, with the following dataframe I want to keep only user 1 and 2 as their values go from 1
to 0
.
Relevant rows
- Row 6 to 7 for user 1
- Row 9 to 10 for user 2
user value
0 0 0
1 0 0
2 0 1
3 0 1
4 1 0
5 1 1
6 1 1
7 1 0
8 2 1
9 2 1
10 2 0
11 2 0
Desired Result
user value
4 1 0
5 1 1
6 1 1
7 1 0
8 2 1
9 2 1
10 2 0
11 2 0
I have tried window functions and conditions but for some reason I cannot get the desired result.
Solution
Let us try cummax
df.loc[df.user.isin(df.loc[df.value != df.groupby('user')['value'].cummax(),'user'])]
Out[769]:
user value
4 1 0
5 1 1
6 1 1
7 1 0
8 2 1
9 2 1
10 2 0
11 2 0
Answered By – BENY
Answer Checked By – David Goodson (BugsFixing Volunteer)