Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas dataframe query single quotes in a string argument

Tags:

python

pandas

How can I escape the single quotes character in the string I want to search for?

Example:

strng = "King's palace"
df.query("fieldname == %s" %(strng))

This query is not returning data because of the quotes. Escaping is not helping.

like image 740
Joe Cheri Ross Avatar asked Oct 23 '25 04:10

Joe Cheri Ross


1 Answers

Use this solution:

# Test data
df = pd.DataFrame({'fieldname': ['King\'s palace', 'Hilton']})

strng = "King's palace"
df.query("fieldname == @strng")

Output:

       fieldname
0  King's palace
like image 113
Romain Avatar answered Oct 24 '25 17:10

Romain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!