Search code examples
pandascategorical-data

How to remove rows with certain values


I want to remove the rows that contain "New York" in city column. I have written the following:

     mydata=mydata[(mydata['city'] != ' New York') 

When I query like below, I do not get any rows back (I checked for different white space variations too)

    mydata[(mydata['city'] == ' New York')

But when I use extra trees, New York is present in the figure...

1- What could be causing this? 2- How can I remove all variations of New York (like ' New York', 'New York ', 'New York')


Solution

  • Use Series.str.strip and change tested value with no trailing space:

    mydata=mydata[(mydata['city'].str.strip() != 'New York') ]