Reputation: 17
I'm having a specific trouble of knowing how to filter values in a set of columns based on certain criteria in pandas df.
I have three columns with binary values in each 'yes/no'. I want to write a code that does the following:
Filter and keep ONLY rows that fulfill minimum of ONE 'yes' but maximum of ONE 'yes'.
So a row that shows: "c1: YES, c2: no: c3: YES"
should be excluded.
A row that shows: "c1: no, c2: no, c3: YES"
should be included.
Thanks in advance!
Upvotes: 0
Views: 436
Reputation: 862611
You can count if there is only on yes
value by compare all columns with sum
and filter in boolean indexing
:
df1 = df[df.eq('yes').sum(axis=1).eq(1)]
Or:
df1 = df[(df == 'yes').sum(axis=1) == 1]
Upvotes: 1