Home > Database > Mysql Tutorial > How Can I Efficiently Insert a Pandas DataFrame into a MySQL Database?

How Can I Efficiently Insert a Pandas DataFrame into a MySQL Database?

DDD
Release: 2024-12-04 06:29:16
Original
623 people have browsed it

How Can I Efficiently Insert a Pandas DataFrame into a MySQL Database?

Inserting Pandas Dataframe into Database via MySQLdb

Connecting to MySQL using Python is straightforward, and operations like row insertion are well-supported. However, when it comes to inserting an entire Pandas dataframe into an existing table, the question arises: is direct insertion possible or does it require iterating over rows?

Direct Insertion using to_sql

The preferred method for inserting a dataframe into MySQL is using the to_sql method:

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

Connection Setup with MySQLdb

To establish a connection to MySQL using MySQLdb, execute the following:

from pandas.io import sql
import MySQLdb

con = MySQLdb.connect()  # may need additional connection parameters
Copy after login

Flavor and if_exists

Setting the flavor of write_frame to 'mysql' enables writing to MySQL. The if_exists parameter controls the behavior when the table already exists, with options being 'fail', 'replace', and 'append'.

Example

Consider a simple table with columns ID, data1, and data2, and a matching Pandas dataframe:

df = pd.DataFrame({'ID': [1, 2, 3], 'data1': ['a', 'b', 'c'], 'data2': [10, 20, 30]})
Copy after login

Inserting this dataframe into the database using the to_sql method would look like this:

sql.write_frame(df, con=con, name='example_table', if_exists='replace', flavor='mysql')
Copy after login

In this example, 'example_table' is the table name in the database, and 'replace' means that the existing table will be replaced with the data from the dataframe.

The above is the detailed content of How Can I Efficiently Insert a Pandas DataFrame into a MySQL Database?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template