如何使用Python创建的数据帧将数据写入Redshift?

如何使用Python创建的数据帧将数据写入Redshift?,第1张

如何使用Python创建数据帧将数据写入Redshift?

您可以

to_sql
用来将数据推送到Redshift数据库。通过使用SQLAlchemy引擎与数据库的连接,我已经能够做到这一点。只要确保
index =False
to_sql
通话中进行设置即可。如果该表不存在,则将创建该表,并且您可以指定是否要调用以替换该表,追加到该表,或者如果该表已存在则失败。

from sqlalchemy import create_engineimport pandas as pdconn = create_engine('postgresql://username:[email protected]:5439/yourdatabase')df = pd.Dataframe([{'A': 'foo', 'B': 'green', 'C': 11},{'A':'bar', 'B':'blue', 'C': 20}])df.to_sql('your_table', conn, index=False, if_exists='replace')

请注意,您可能需要

pip installpsycopg2
通过SQLAlchemy连接到Redshift。

to_sql文档



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5639871.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存