Roooobz
Roooobz

Reputation: 55

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

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?

Upvotes: 2

Views: 2993

Answers (3)

Yaakov Bressler
Yaakov Bressler

Reputation: 12078

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"''')

Upvotes: 2

hd1
hd1

Reputation: 34677

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"''')

Upvotes: 2

gtomer
gtomer

Reputation: 6564

You can do it like that:

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

Upvotes: 2

Related Questions