简体   繁体   English

在MySQL中插入忽略熊猫数据框

[英]insert ignore pandas dataframe into mysql

I want to "insert ignore" an entire pandas dataframe into mysql. 我想将整个熊猫数据帧“插入忽略”到mysql中。 Is there a way to do this without looping over the rows? 有没有一种方法可以在不循环行的情况下进行操作?

In dataframe.to_sql I only see the option if_exists 'append' but will this still continue on duplicate unique keys? 在dataframe.to_sql中,我仅看到if_exists'append'选项,但是在重复的唯一键上仍会继续吗?

Consider using a temp table (with exact structure of final table) that is always replaced by pandas then run the INSERT IGNORE in a cursor call: 考虑使用总是被熊猫替换的临时表(具有最终表的确切结构),然后在游标调用中运行INSERT IGNORE

dataframe.to_sql('myTempTable', con, if_exists ='replace')

cur = con.cursor()
cur.execute("INSERT IGNORE INTO myFinalTable SELECT * FROM myTempTable")
con.commit()

There is no way to do this in pandas till the current version of pandas (0.20.3) . 在当前版本的pandas (0.20.3)之前,在熊猫中无法执行此操作。

The option if_exists applies only on table ( not on rows ) as stated in the documentation . 文档中所述, if_exists选项if_exists适用于表(不适用于行)。

if_exists : {‘fail’, ‘replace’, ‘append’}, default ‘fail’

fail : If table exists, do nothing. fail :如果存在,则什么也不做。

replace : If table exists, drop it, recreate it, and insert data. replace :如果存在 ,则将其删除,重新创建并插入数据。

append : If table exists, insert data. append :如果存在 ,则插入数据。 Create if does not exist. 如果不存在则创建。

Via Looping 通过循环

This will slow down the process as you are inserting one row at a time 一次插入一行将减慢该过程

for x in xrange(data_frame.shape[0]):
    try:
        data_frame.iloc[x:x+1].to_sql(con=sql_engine, name="table_name", if_exists='append')
    except IntegrityError:
        # Your code to handle duplicates
        pass 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM