【发布时间】:2018-11-01 07:45:02
【问题描述】:
我有一个带有索引的数据框,我想将它存储在 postgresql 数据库中。为此我使用df.to_sql(table_name,engine,if_exists='replace', index=True,chunksize=10000)
pandas 数据框中的索引列被复制到数据库中,但未设置为主键。
有两种解决方案需要额外的步骤:
- 指定架构
df.to_sql(schema=)docs -
在表被摄取后设置主键。查询:
ALTER TABLE table_name 添加主键(id_column_name)
有没有办法在不指定架构或更改表的情况下设置主键?
【问题讨论】:
-
This question has a couple suggestions for setting the primary key in a MySQL table, which would likely apply to postgresql as well 但是,似乎简单地添加一行以使用 ALTER TABLE 将是设置主键的最快和最简单的方法。
标签: postgresql pandas sqlalchemy