How do I drop every row that doesn't contain one of 2 values?

Viewed 20

I am struggling to drop rows that do not contain certain data. For example, I want my dataset to only predict between 'Male' and 'Female' so I need to drop everything with the 'Gender' column that is not equal to those two choices. How would I go about doing this?

I've tried:

df.drop(df['GENDER'] != 'Male' & 'Female')

I honestly have tried more code that I can type out so any help would be great. All of the pandas documentation I have read only shows how to remove certain values but not how to remove everything but certain values.

Thanks in advance!

2 Answers

Here you go

df = df[df.GENDER.isin(['Male',"Female"])]
df = df[~df['GENDER'].isin(['Male',"Female"])]
Related