需要先安裝3個第三方包
首先,連接數據庫並執行sql
import pymysql import pandas as pd from sqlalchemy import create_engine #Python中一個通過 ORM 操作數據庫的框架 def reader(sql): engine = create_engine('mysql+pymysql://root:123456@localhost:3306/crm_info?charset=utf8') data = pd.read_sql(sql,engine) return data pddata = reader("select * from zj_zjxx") pddata.head()
使用pandas中DataFrame.to_sql()可以將數據導入到數據庫表中
import pymysql import pandas as pd ym = pd.read_csv("C:\\Users\\52745\\Jupyter_doc\\ym\\廣東省.csv") ym.to_sql(name = 'ym2',con = 'mysql+pymysql://root:123456@localhost:3306/crm_info?charset=utf8',if_exists="append",index=False)
