One - One Code All

Blog Content

pandas的to_sql的坑if_exists=append

Python 统计学-科学计算   2020-07-19 18:43:47

pandas.to_sql

import pandas as pd
from sqlalchemy import create_engine
conn = create_engine('mysql+mysqldb://root:password@localhost:3306/databasename?charset=utf8') 
pd.io.sql.to_sql(df1,tablename,con=conn,if_exists='repalce')
df.to_sql(tablename,
            con=engine,
            if_exists='append', 
            index=False)


if_exists字段:


fail:如果表存在,啥也不做

replace: 如果表存在,删了表,再建立一个新表,把数据插入

append:如果表存在,把数据插入,如果表不存在创建一个表!!




上一篇:Python 操作Excel ,openpyxl

The minute you think of giving up, think of the reason why you held on so long.