Python/Pandas/PyMySQL-%s语法

2024-10-01 09:24:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着做以下手术:

import pymsql
import pandas as pd

df:

     id      height_code        name          ...
1    34      123123             Jesus
2    84      3421               Heaps
3    23      45243              Ornitorrincus
...

connection=pymysql.connect(host=xxx,user=xxxx,password=xxxx,db=xxxx)

for i in df.index:
    df2=pd.read_sql("select business_id,name,color from table123 where business_id=%s;",connection) % df['id'][i]

这里的想法是只使用business_id = df['id'][i]中的行访问一个巨大的sql和filter table123

但是,我得到了以下错误:

^{pr2}$

所以,显然我在pymsql中的%ssynthax有问题。试着看一下他们的手册,但没有找到一个很好的解释。在

有人能帮忙吗?在


Tags: nameimportidpandasdfsqlasbusiness