import pymysql
import pandas as pd
db = pymysql.connect('localhost', 'testuser', 'test123', 'world')
df1 = pd.read_sql('select * from country limit 5', db)
df1
我需要使用 country2
创建一个表名称,并将 df1 更新为 country2
最佳答案
使用 Pandas to_sql
(https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html)。这应该适合你:
import mymysql
from sqlalchemy import create_engine
sql_table_name = 'country2'
engine = create_engine("mysql://testuser:test123@localhost:0/world") # creat engine
df1.to_sql(sql_table_name, engine) # add to table
关于python - 如何将数据帧输出插入到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59180242/