如何通过mysqldb将pandas数据框插入到数据库中?

How to insert pandas dataframe via mysqldb into database?(如何通过mysqldb将pandas数据框插入到数据库中?)
本文介绍了如何通过mysqldb将pandas数据框插入到数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我可以从 python 连接到我的本地 mysql 数据库,我可以创建、选择和插入单个行.

I can connect to my local mysql database from python, and I can create, select from, and insert individual rows.

我的问题是:我可以直接指示 mysqldb 获取整个数据帧并将其插入现有表中,还是需要遍历行?

My question is: can I directly instruct mysqldb to take an entire dataframe and insert it into an existing table, or do I need to iterate over the rows?

在这两种情况下,对于具有 ID 和两个数据列以及匹配数据框的非常简单的表,python 脚本会是什么样子?

In either case, what would the python script look like for a very simple table with ID and two data columns, and a matching dataframe?

推荐答案

更新:

现在有一个 to_sql 方法,这是执行此操作的首选方法,而不是 write_frame:

df.to_sql(con=con, name='table_name_for_df', if_exists='replace', flavor='mysql')

另请注意:pandas 0.14 中的语法可能会发生变化...

您可以设置与MySQLdb的连接:

from pandas.io import sql
import MySQLdb

con = MySQLdb.connect()  # may need to add some other options to connect

write_frameflavor设置为'mysql'表示可以写入mysql:

Setting the flavor of write_frame to 'mysql' means you can write to mysql:

sql.write_frame(df, con=con, name='table_name_for_df', 
                if_exists='replace', flavor='mysql')

参数if_exists告诉pandas如果表已经存在如何处理:

The argument if_exists tells pandas how to deal if the table already exists:

if_exists: {'fail', 'replace', 'append'},默认 'fail'
    fail:如果表存在,则什么都不做.
    replace:如果表存在,删除它,重新创建它,然后插入数据.
    append:如果表存在,插入数据.不存在则创建.

if_exists: {'fail', 'replace', 'append'}, default 'fail'
     fail: If table exists, do nothing.
     replace: If table exists, drop it, recreate it, and insert data.
     append: If table exists, insert data. Create if does not exist.

虽然 write_frame docs 目前建议它只适用于 sqlite,mysql 似乎受支持,实际上有相当多的 代码库中的mysql测试.

Although the write_frame docs currently suggest it only works on sqlite, mysql appears to be supported and in fact there is quite a bit of mysql testing in the codebase.

这篇关于如何通过mysqldb将pandas数据框插入到数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)