Write fast pandas dataframe to postgres(将快速 pandas 数据帧写入 postgres)
问题描述
我想知道将数据从 pandas DataFrame 写入 postges DB 表的最快方法.
I wonder of the fastest way to write data from pandas DataFrame to table in postges DB.
1)我试过pandas.to_sql
,但由于某种原因它需要实体来复制数据,
1) I've tried pandas.to_sql
, but for some reason it takes entity to copy data,
2) 除了我尝试过以下操作:
2) besides I've tried following:
import io
f = io.StringIO()
pd.DataFrame({'a':[1,2], 'b':[3,4]}).to_csv(f)
cursor = conn.cursor()
cursor.execute('create table bbbb (a int, b int);COMMIT; ')
cursor.copy_from(f, 'bbbb', columns=('a', 'b'), sep=',')
cursor.execute("select * from bbbb;")
a = cursor.fetchall()
print(a)
cursor.close()
但它返回空列表[]
.
所以我有两个问题:将数据从 python 代码(数据帧)复制到 postgres DB 的最快方法是什么?我尝试过的第二种方法有什么不正确的地方?
So I have two questions: what is the fastest way to copy data from python code (dataframe) to postgres DB? and what was incorrect in the second approach that I've tried?
推荐答案
您的第二种方法应该非常快.
Your second approach should be very fast.
你的代码有两个问题:
- 将 csv 写入
f
后,您将位于文件末尾.在开始阅读之前,您需要将位置放回到开头. - 写csv时,需要省略header和index
- After writing the csv to
f
you are positioned at the end of the file. You need to put your position back to the beginning before starting to read. - When writing a csv, you need to omit the header and index
你的最终代码应该是这样的:
Here is what your final code should look like:
import io
f = io.StringIO()
pd.DataFrame({'a':[1,2], 'b':[3,4]}).to_csv(f, index=False, header=False) # removed header
f.seek(0) # move position to beginning of file before reading
cursor = conn.cursor()
cursor.execute('create table bbbb (a int, b int);COMMIT; ')
cursor.copy_from(f, 'bbbb', columns=('a', 'b'), sep=',')
cursor.execute("select * from bbbb;")
a = cursor.fetchall()
print(a)
cursor.close()
这篇关于将快速 pandas 数据帧写入 postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将快速 pandas 数据帧写入 postgres


- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01