将Pandas Dataframe写入MySQL [英] Writing a Pandas Dataframe to MySQL

查看:208
本文介绍了将Pandas Dataframe写入MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Python Pandas Dataframe写入MySQL数据库.我意识到可以为此使用sqlalchemy ,但是我我想知道是否还有另一种可能更容易的方法,最好是已经内置在Pandas中了.我花了很多时间尝试使用For循环来完成此操作,但这是无法实现的.

I'm trying to write a Python Pandas Dataframe to a MySQL database. I realize that it's possible to use sqlalchemy for this, but I'm wondering if there is another way that may be easier, preferably already built into Pandas. I've spent quite some time trying to do it with a For loop, but it's not realiable.

如果有人知道更好的方法,将不胜感激.

If anyone knows of a better way, it would be greatly appreciated.

非常感谢!

推荐答案

sqlalchemy 的另一个选项可以使用 to_sql ,但将来将不推荐使用,但现在版本 pandas 0.18.1文档仍处于活动状态.

The other option to sqlalchemy can be used to_sql but in future released will be deprecated but now in version pandas 0.18.1 documentation is still active.

根据pandas文档 pandas.DataFrame.to_sql 您可以使用以下语法:

According to pandas documentation pandas.DataFrame.to_sql you can use following syntax:

DataFrame.to_sql(name, con, flavor='sqlite', schema=None, if_exists='fail', index=True, index_label=None, chunksize=None, dtype=None)

您指定con 类型/模式和风味"mysql" ,下面是一些说明:

you specify the con type/mode and flavor ‘mysql’, here is some description:

con:SQLAlchemy引擎或DBAPI2连接(传统模式) 使用SQLAlchemy可以使用该库支持的任何数据库.如果是DBAPI2对象,则仅支持sqlite3.

con : SQLAlchemy engine or DBAPI2 connection (legacy mode) Using SQLAlchemy makes it possible to use any DB supported by that library. If a DBAPI2 object, only sqlite3 is supported.

flavor:{'sqlite','mysql'},默认为'sqlite' 使用.使用SQLAlchemy引擎时被忽略.不建议使用"mysql", 将在以后的版本中删除,但会得到进一步支持 通过SQLAlchemy引擎.

flavor : {‘sqlite’, ‘mysql’}, default ‘sqlite’ The flavor of SQL to use. Ignored when using SQLAlchemy engine. ‘mysql’ is deprecated and will be removed in future versions, but it will be further supported through SQLAlchemy engines.

这篇关于将Pandas Dataframe写入MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆