Niche.P Niche.P - 11 months ago 42
Python Question

Displaying rows according to Specific Values

I want to select several rows according to the attributes in Type column.

Let's pretend I have this dataframe:

Type | Killed | Survive
Dog 1 0
Cat 3 5
Dog 4 1
Cow 2 4
Fish 1 3

I would like to select the row that has Type = ['Dog', 'Cat', 'Fish']

This would be my desire result:

Type | Killed | Survived
Dog 1 0
Dog 4 1
Cat 3 5
Fish 1 3

I know that you can use :

df[df['Type'] == 'Dog']

to get dog only.

but I would like to know how to select more than one type.

I have tried this but it doesn't work:

df[df['Type'] == 'Dog', 'Cat', 'Fish']

Thanks for helping me guys!

Answer Source

You can use isin:

df[df['Type'].isin(['Dog', 'Cat', 'Fish'])]