Remove rows from pandas DataFrame based on condition
28,718
General boolean indexing
df[df['Species'] != 'Cat']
# df[df['Species'].ne('Cat')]
Index Name Species
1 1 Jill Dog
3 3 Harry Dog
4 4 Hannah Dog
df.query
df.query("Species != 'Cat'")
Index Name Species
1 1 Jill Dog
3 3 Harry Dog
4 4 Hannah Dog
For information on the pd.eval()
family of functions, their features and use cases, please visit Dynamic Expression Evaluation in pandas using pd.eval().
df.isin
df[~df['Species'].isin(['Cat'])]
Index Name Species
1 1 Jill Dog
3 3 Harry Dog
4 4 Hannah Dog
Author by
James Geddes
DevOps professional, Python & Terraform obsessive
Updated on June 18, 2020Comments
-
James Geddes almost 4 years
I am a newbie to pandas so please forgive the newbie question!
I have the following code;
import pandas as pd pet_names = ["Name","Species" "Jack","Cat" "Jill","Dog" "Tom","Cat" "Harry","Dog" "Hannah","Dog"] df = pd.DataFrame(pet_names) df = df[df['Species']!='Cat'] print(df)
I would like to remove all the rows that contain "Cat" in the "Species" column, leaving all the dogs behind. How do I do this? Unfortunately, this code is currently returning errors.