vilsef
vilsef

Reputation: 17

How to filter columns based on specific values in pandas dataframe

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.

enter image description here

Thanks in advance!

Upvotes: 0

Views: 436

Answers (1)

jezrael
jezrael

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

Related Questions