Pandas.DataFrame具有to_sql方法,该方法将数据帧写入sql表。
使用该方法的最简单方法是使用sqlalchemy创建连接(您需要安装mysql-python),然后使用.to_sql该方法将数据读入表中。
from sqlalchemy import create_engine
engine = create_engine('mysql://username:password@host:port/database') #change to connect your mysql
#if you want to append the data to an existing table
df.to_sql(name='SQL Table name',con=engine,if_exists='append',index=False)
#if you want to create a new table
df.to_sql(name='New SQL Table name',con=engine,if_exists='fail',index=False)
Run Code Online (Sandbox Code Playgroud)
请注意,dtype如果您事先创建了表,则需要使用param 来定义表列的dtype。
| 归档时间: |
|
| 查看次数: |
3361 次 |
| 最近记录: |