I can connect to my local mysql database from python, and I can create, select from, and insert individual rows.
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?
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?
Update:
There is now a
to_sql
method, which is the preferred way to do this, rather thanwrite_frame
:Also note: the syntax may change in pandas 0.14...
You can set up the connection with MySQLdb:
Setting the
flavor
ofwrite_frame
to'mysql'
means you can write to mysql:The argument
if_exists
tells pandas how to deal if the table already exists: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.You can do it by using pymysql:
For example, let's suppose you have a MySQL database with the next user, password, host and port and you want to write in the database 'data_2', if it is already there or not.
If you already have the database created:
If you do NOT have the database created, also valid when the database is already there:
Similar threads:
The to_sql method works for me.
However, keep in mind that the it looks like it's going to be deprecated in favor of SQLAlchemy:
You might output your
DataFrame
as a csv file and then usemysqlimport
to import your csv into yourmysql
.EDIT
Seems pandas's build-in sql util provide a
write_frame
function but only works in sqlite.I found something useful, you might try this
Andy Hayden mentioned the correct function (
to_sql
). In this answer, I'll give a complete example, which I tested with Python 3.5 but should also work for Python 2.7 (and Python 3.x):First, let's create the dataframe:
Which gives:
To import this dataframe into a MySQL table:
One trick is that MySQLdb doesn't work with Python 3.x. So instead we use
mysqlconnector
, which may be installed as follows:Output:
Note that
to_sql
creates the table as well as the columns if they do not already exist in the database.