把pandas表格数据DataFrame类型的数据写入mysql数据库

 
import pandas as pd
import pymysql
from sqlalchemy import create_engine

yconnect = create_engine('mysql+pymysql://root:[email protected]:3306/datepi?charset=utf8')
qiyetimelongsql='''select did,manmadedate,avg(timelong) from qiye where timelong is not null group by did,manmadedate'''
# qiyetimelong=pd.read_sql(qiyetimelongsql,yconnect)

##将数据写入mysql的数据库,但需要先通过sqlalchemy.create_engine建立连接,且字符编码设置为utf8,否则有些latin字符不能处理
pd.io.sql.to_sql(pd.read_sql(qiyetimelongsql,yconnect), 'qiyetimelong', yconnect, schema='datawarehouse', if_exists='append')

你可能感兴趣的:(Pandas,Mysql)