How to Use a Wildcard (%) in Pandas read_sql()

只谈情不闲聊 提交于 2021-02-16 05:17:38

问题


I am trying to run a MySQL query that has a text wildcard in as demonstrated below:

import sqlalchemy
import pandas as pd

#connect to mysql database
engine = sqlalchemy.create_engine('mysql://user:@localhost/db?charset=utf8')
conn = engine.connect()

#read sql into pandas dataframe
mysql_statement = """SELECT * FROM table WHERE field LIKE '%part%'; """
df = pd.read_sql(mysql_statement, con=conn)

When run I get the error as shown below related to formatting.

TypeError: not enough arguments for format string

How can I use a wild card when reading MySQL with Pandas?


回答1:


from sqlalchemy import create_engine, text
import pandas as pd

mysql_statement = """SELECT * FROM table WHERE field LIKE '%part%'; """
df = pd.read_sql( text(mysql_statement), con=conn)

You can use text() function from sqlalchemy




回答2:


Under the hood of pandas, it's using whatever sql engine you've given it to parse the statement. In your case that's sqlalchemy, so you need to figure out how it handles %. It might be as easy as escaping it with LIKE '%%part%%'.

In the case of psycopg, you use the params variable like this:

mysql_statement = """SELECT * FROM table WHERE field LIKE %s; """
df = pd.read_sql(mysql_statement, con=conn, params=("%part%",))



回答3:


you need to use params options in read_sql pandas method.

# string interpolate 
mysql_statement = """SELECT * FROM table WHERE field LIKE %s; """
df = pd.read_sql(mysql_statement, con=conn, params=("%part%",))

# variable interpolate 
# string interpolate 
val = 'part'
mysql_statement = """SELECT * FROM table WHERE field LIKE %s; """
df = pd.read_sql(mysql_statement, con=conn, params=('%' + val + '%',))



回答4:


'select * from _table_ where "_column_name_" like 'somethin%' order by... '

Putting "" around the _column_name_ solved this problem for me.



来源:https://stackoverflow.com/questions/40442292/how-to-use-a-wildcard-in-pandas-read-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!