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
fyou 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
基础教程推荐
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
