<tfoot id='BapEd'></tfoot>

<small id='BapEd'></small><noframes id='BapEd'>

  • <i id='BapEd'><tr id='BapEd'><dt id='BapEd'><q id='BapEd'><span id='BapEd'><b id='BapEd'><form id='BapEd'><ins id='BapEd'></ins><ul id='BapEd'></ul><sub id='BapEd'></sub></form><legend id='BapEd'></legend><bdo id='BapEd'><pre id='BapEd'><center id='BapEd'></center></pre></bdo></b><th id='BapEd'></th></span></q></dt></tr></i><div id='BapEd'><tfoot id='BapEd'></tfoot><dl id='BapEd'><fieldset id='BapEd'></fieldset></dl></div>

        • <bdo id='BapEd'></bdo><ul id='BapEd'></ul>
      1. <legend id='BapEd'><style id='BapEd'><dir id='BapEd'><q id='BapEd'></q></dir></style></legend>
      2. 将快速 pandas 数据帧写入 postgres

        Write fast pandas dataframe to postgres(将快速 pandas 数据帧写入 postgres)

        • <bdo id='Crfwa'></bdo><ul id='Crfwa'></ul>
          1. <i id='Crfwa'><tr id='Crfwa'><dt id='Crfwa'><q id='Crfwa'><span id='Crfwa'><b id='Crfwa'><form id='Crfwa'><ins id='Crfwa'></ins><ul id='Crfwa'></ul><sub id='Crfwa'></sub></form><legend id='Crfwa'></legend><bdo id='Crfwa'><pre id='Crfwa'><center id='Crfwa'></center></pre></bdo></b><th id='Crfwa'></th></span></q></dt></tr></i><div id='Crfwa'><tfoot id='Crfwa'></tfoot><dl id='Crfwa'><fieldset id='Crfwa'></fieldset></dl></div>

            <small id='Crfwa'></small><noframes id='Crfwa'>

                <tbody id='Crfwa'></tbody>
              <tfoot id='Crfwa'></tfoot>

            • <legend id='Crfwa'><style id='Crfwa'><dir id='Crfwa'><q id='Crfwa'></q></dir></style></legend>
                  本文介绍了将快速 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.

                  你的代码有两个问题:

                  1. 将 csv 写入 f 后,您将位于文件末尾.在开始阅读之前,您需要将位置放回到开头.
                  2. 写csv时,需要省略header和index
                  1. 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.
                  2. 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                  <i id='Z1LcU'><tr id='Z1LcU'><dt id='Z1LcU'><q id='Z1LcU'><span id='Z1LcU'><b id='Z1LcU'><form id='Z1LcU'><ins id='Z1LcU'></ins><ul id='Z1LcU'></ul><sub id='Z1LcU'></sub></form><legend id='Z1LcU'></legend><bdo id='Z1LcU'><pre id='Z1LcU'><center id='Z1LcU'></center></pre></bdo></b><th id='Z1LcU'></th></span></q></dt></tr></i><div id='Z1LcU'><tfoot id='Z1LcU'></tfoot><dl id='Z1LcU'><fieldset id='Z1LcU'></fieldset></dl></div>
                    <bdo id='Z1LcU'></bdo><ul id='Z1LcU'></ul>
                          <tbody id='Z1LcU'></tbody>
                        <tfoot id='Z1LcU'></tfoot>

                          <small id='Z1LcU'></small><noframes id='Z1LcU'>

                            <legend id='Z1LcU'><style id='Z1LcU'><dir id='Z1LcU'><q id='Z1LcU'></q></dir></style></legend>