【发布时间】:2021-05-28 23:08:25
【问题描述】:
假设我有一个数据框 df,它具有与 sql 表相同的列。 将数据框插入表中的最简单(也是最有效的方法)是什么。
import pandas as pd
from sqlalchemy import create_engine
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
df = pd.DataFrame({'name' : ['User 1', 'User 2', 'User 3']})
engine = create_engine('sqlite:///database.db')
conn = engine.connect()
Base = automap_base()
Base.prepare(engine, reflect=True)
session = Session(bind=engine)
# No assume that the database has a table Users with one column "name"
# is there a function, which adds all entries of the dataframe to the sql table?
# This should also works with dates/floats type of columns and multiple columns
【问题讨论】:
标签: python pandas sqlalchemy