C_psy
C_psy

Reputation: 647

Conditionally delete rows by ID in Pandas

I have a pandas df:

ID Score C  D 
1  2     x  y
1  nan   x  y
1  2     x  y
2  3     x  y
2  2     x  y
3  2     x  y
3  4     x  y
3  3     x  y

For each ID, like to remove rows where df.Score = 2 but only when there is a 3 or 4 present for that ID. I'd like to keep nansand 2 when the only score per ID = 2.

So I get:

ID Score C  D 
1  2     x  y
1  nan   x  y
1  2     x  y
2  3     x  y
3  4     x  y
3  3     x  y

Any help, much appreciated

Upvotes: 1

Views: 107

Answers (1)

anky
anky

Reputation: 75100

Use:

df[~df.groupby('ID')['Score'].apply(lambda x:x.eq(2)&x.isin([3,4]).any())]

   ID  Score  C  D
0   1    2.0  x  y
1   1    NaN  x  y
2   1    2.0  x  y
3   2    3.0  x  y
6   3    4.0  x  y
7   3    3.0  x  y

Upvotes: 2

Related Questions