简体   繁体   中英

pandas dataframe query single quotes in string argument

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

ex.

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

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

What about using this solution.

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

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

       fieldname
0  King's palace

I tried giving this as df.query(r'fieldname == %s' %(strng)) . It worked. Thanks Edchum.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM