I have this DataFrame:
id outcome
1 3 no
2 3 yes
3 3 no
4 6 no
5 6 yes
6 6 no
7 7 no
8 7 yes
9 7 no
10 7 no
11 7 yes
However, would like to create a new one based on the row above a 'yes' (dismissing anything after).
They must also be grouped by the id column.
This should be the output:
id outcome
1 3 no
4 6 no
7 7 no
10 7 no
The problem is if there isn't a row above a 'yes', I can't do anything with the following code. I also need to groupby id. Any suggestions?
df = pd.DataFrame(data={'id':[3,3,3,3,3,5,5,5,5,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7],
'outcome': ['no','no','no','yes','no','no','no','yes','yes','no','no','yes','yes','yes','yes','yes','no','no','no', 'no', 'yes', 'no', 'no', 'yes']})
pre_df = df[df['outcome'].eq('yes').shift(-1)]