Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to trim a string inside a query string passed to the Pandas query function?

I need to trim the ‘My Pet’ column inside the query string. How can that be done?

# Note that one cat literal has a trailing space.
testDF = pd.DataFrame([{"My Pet":"cat ",   "Cost":"$10 ", "Weight":"10 pounds", "Name":"Violet"},
                       {"My Pet":"cat",    "Cost":"$10 ", "Weight":"15 pounds", "Name":"Sirius"},
                       {"My Pet":"dog",    "Cost":"$0 ",  "Weight":"50 pounds", "Name":"Sam"},
                       {"My Pet":"turtle", "Cost":"$5 ",  "Weight":"20 ounces", "Name":"Tommy"},
                      ])
# We try to filter on cat. 
catDF = testDF.query("`My Pet` == 'cat'")  # This yields only one row because one cat cell has a trailing space
catDF.head()

Output is only one row but I would like to get both rows with cat in them

    My Pet  Cost    Weight  Name
1   cat     $10     15 pounds   Sirius

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use Series.str.strip, but need engine="python":

catDF = testDF.query("`My Pet`.str.strip() == 'cat'", engine="python")
print (catDF)
  My Pet  Cost     Weight    Name
0   cat   $10   10 pounds  Violet
1    cat  $10   15 pounds  Sirius
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading