How to execute SQL cell in CODE loop and merge result?

This is my ugly way to implement:

from sqlalchemy import create_engine

engine = create_engine('mysql+pymysql://admin:123456@127.0.0.1:8888/test')
crossDf = pd.DataFrame(columns=['company'])
for i in range(0,len(df),1000):
    compList = df.loc[i:i+1000,"company"].tolist()
    compList = map(lambda x:"'"+x+"'",compList)
    compListStr = "(" + ",".join(compList) + ")"
    findDf = pd.read_sql(f"select company from test_table where company in {compListStr}  group by company", engine)
    crossDf = crossDf.append(findDf,ignore_index=True)

crossDf

But this way doesn’t use attached data or SQL cell,I wonder if there is a better way to implement this?

1 Like