5

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.

cs95
  • 330,695
  • 80
  • 606
  • 657
James Geddes
  • 532
  • 3
  • 7
  • 30
  • 1
    Possible duplicate of [Deleting DataFrame row in Pandas based on column value](https://stackoverflow.com/questions/18172851/deleting-dataframe-row-in-pandas-based-on-column-value) – CodeLikeBeaker Aug 03 '17 at 16:29

1 Answers1

31

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
Community
  • 1
  • 1
cs95
  • 330,695
  • 80
  • 606
  • 657
  • 1
    I just upvoted you four times for each answer. Too bad, SO didn't recognize those clicks. – Scott Boston Aug 03 '17 at 16:50
  • "AttributeError: 'DataFrame' object has no attribute 'Species'" - what am I doing wrong? – James Geddes Aug 03 '17 at 16:51
  • 2
    @JamesGeddes If you are using the same data, as in your post, that's why. You have issues with your input data. Try this: `pet_names = [["Jack","Cat"], ["Jill","Dog"], ["Tom","Cat"], ["Harry","Dog"] ,["Hannah","Dog"] ]; df = pd.DataFrame(pet_names,columns =["Name","Species"])` (from Bharath's post) – cs95 Aug 03 '17 at 16:53
  • @ScottBoston hahaha if only! – cs95 Aug 03 '17 at 16:55
  • 1
    Multiple choices +1 – Bharath Aug 03 '17 at 17:00
  • 1
    now you have my upvote ~Also recommend `df.query`, especially for multiple index – BENY Aug 03 '17 at 17:27