Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 948
Jason Melo Hall Avatar asked Nov 05 '16 19:11

Jason Melo Hall


3 Answers

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

like image 69
Mubbashar Avatar answered Nov 14 '22 21:11

Mubbashar


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%",))
like image 37
ZakJ Avatar answered Nov 14 '22 21:11

ZakJ


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 + '%',))
like image 41
pritish Avatar answered Nov 14 '22 23:11

pritish