pandas+sqlalchemy将数据导入MySQL

pandas将数据导入MySQL

import pandas as pd
from sqlalchemy import create_engine


db_info = {
    
    'user': 'root',
           'password': 'root',
           'host': 'localhost',
           'port': 3306,
           'database': 'test'
           }
engine = create_engine('mysql+pymysql://%(user)s:%(password)s@%(host)s:%(port)d/%(database)s?charset=utf8' % db_info,
                       encoding='utf-8')
# 上面的复制就行
# 读数据
df = pd.read_excel(r'C:\Users\Administrator\Desktop\test.xlsx')
# df=df.set_index('word')
print(df)

# 写入数据库
pd.io.sql.to_sql(df, name='trend',con=engine, if_exists='replace', index=False)
# 关闭
engine.dispose()

猜你喜欢

转载自blog.csdn.net/weixin_44429965/article/details/115384066