What I have
A B C D E 0 foo 0 1.2 1 2 1 foo 1 1.3 2 4 2 foo 2 2.1 4 5 3 foo 3 3.1 3 5 4 nan 0 0 0 0 5 bar 0 4.1 4 6 6 bar 1 1.2 5 8 7 bar 2 1.4 6 9 8 bar 3 5.0 7 9 9 nan 0 0 0 0 10 baz 0 4.1 5 0 11 baz 1 1.2 5 3 12 baz 2 1.4 6 9 13 baz 3 5.0 7 9
What I want, select first occurrence where D >= 4 for each A(key)
So end result will look like,
A B C D E 0 foo 2 2.1 4 5 1 bar 0 4.1 4 6 2 baz 0 4.1 5 0
Advertisement
Answer
You can first slice the rows that match the condition on D, then groupby
A and get the first
element of each group:
df[df['D'].ge(4)].groupby('A', sort=False).first()
output:
B C D E A foo 2 2.1 4 5 bar 0 4.1 4 6 baz 0 4.1 5 0