How can I query a string that has an apostrophe from a pandas dataframe?

Question:

So I’ve got a pandas dataframe and I would like to select certain rows from it based on particular values in columns.
The following code works for me:

df.query('Col1 == "zz" and Col2 == "yy"')

and returns all the rows that have "zz" in Col1 and "yy" in Col2. However, I run into a problem if the string contains an apostrophe. e.g.

df.query('Col1 == "zz" and Col2 == "yy's"')

python throws a syntax error. I have considered just removing the apostrophe from the string but it is a location name and would be more correct to keep it. How can I fix this/what can I use instead?

Asked By: Roooobz

||

Answers:

You can do it like that:

df.query('Col1 == "zz" and Col2 == "yy's"')
Answered By: gtomer

Escaping special characters like the previous answer is a solid approach. Another would be to use multi-line quotes:

df.query('''Col1 == "zz" and Col2 == "yy's"''')
Answered By: Yaakov Bressler

Your issue is that python is interpreting the apostrophe as a special character. Hence, the solution is to triple quote the query string, like the following:

df.query('''Col1 == "zz" and Col2 == "yy's"''')
Answered By: hd1
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.