<tfoot id='PVnEU'></tfoot>

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

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

          <bdo id='PVnEU'></bdo><ul id='PVnEU'></ul>
      1. PYODBC 到 Pandas - DataFrame 不起作用 - 传递值的形状是(x,y),索引暗示(w,z)

        时间:2023-09-29
          <bdo id='QsCT6'></bdo><ul id='QsCT6'></ul>

            <tbody id='QsCT6'></tbody>

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

                1. <tfoot id='QsCT6'></tfoot>
                2. <legend id='QsCT6'><style id='QsCT6'><dir id='QsCT6'><q id='QsCT6'></q></dir></style></legend>

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

                4. 本文介绍了PYODBC 到 Pandas - DataFrame 不起作用 - 传递值的形状是(x,y),索引暗示(w,z)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我以前使用 pyodbc 和 python,但现在我已经将它安装在一台新机器上(win 8 64 位,Python 2.7 64 位,PythonXY 和 Spyder).

                  I used pyodbc with python before but now I have installed it on a new machine ( win 8 64 bit, Python 2.7 64 bit, PythonXY with Spyder).

                  以前我用过(在底部可以找到更多真实的例子):

                  Before I used to (at the bottom you can find more real examples):

                  columns = [column[0] for column in cursor.description]
                  temp = cursor.fetchall()
                  data = pandas.DataFrame(temp,columns=columns)
                  

                  它会正常工作.现在似乎 DataFrame 不再能够从从游标中获取的数据进行转换了.它返回:

                  and it would work fine. Now it seems like DataFrame is not able to convert from the data fetched from the cursor anymore. It returns:

                  传递值的形状是 (x,y),索引表示 (w,z)

                  Shape of passed values is (x,y), indices imply (w,z)

                  我有点明白问题出在哪里.基本上,想象我只取一行.然后 DataFrame 想对其进行整形(1,1),只有一个元素.虽然我想要 (1,X) 其中 X 是列表的长度.

                  I kind of see where the issue is. Basically, imagine I fetch only one row. Then DataFrame would like to shape it (1,1), one element only. While I would like to have (1,X) where X is the length of the list.

                  我不确定为什么行为会改变.也许是我拥有的 Pandas 版本,或者 pyodbc,但更新是有问题的.我试图更新一些模块,但它搞砸了一切,我使用的任何方法(二进制文件 - 用于正确的机器/安装 - pip install,easy-install,任何东西!等等......这确实非常令人沮丧.我可能会避免从现在开始为 Python Win 8 64 位).

                  I am not sure why the behavior changed. Maybe it is the Pandas version I have, or the pyodbc, but updating is problematic. I tried to update some modules but it screws up everything, any method I use (binaries--for the right machine/installation--pip install, easy-install,anything! etc.. which is very frustrating indeed. I would probably avoid Win 8 64 bit from now on for Python).

                  实例:

                  sql = 'Select * form TABLE'
                  cursor.execute(sql)
                  columns = [column[0] for column in cursor.description]
                  data    = cursor.fetchall()
                          con.close()
                              results = DataFrame(data, columns=columns)
                  

                  返回:* ValueError: 传递值的形状是 (1, 1540),索引暗示 (51, 1540)

                  Returns: * ValueError: Shape of passed values is (1, 1540), indices imply (51, 1540)

                  注意:

                  ipdb> type(data)
                  <type 'list'>
                  ipdb> np.shape(data)
                  (1540, 51)
                  ipdb> type(data[0])
                  <type 'pyodbc.Row'>
                  

                  现在,例如,如果我们这样做:

                  Now, for example, if we do:

                  ipdb> DataFrame([1,2,3],columns=['a','b','c'])
                  

                  * ValueError: 传递值的形状是 (1, 3),索引意味着 (3, 3)

                  * ValueError: Shape of passed values is (1, 3), indices imply (3, 3)

                  如果我们这样做:

                  ipdb> DataFrame([[1,2,3]],columns=['a','b','c'])
                  

                  a b c0 1 2 3

                  a b c 0 1 2 3

                  但是,即使尝试:

                  ipdb> DataFrame([data[0]], columns=columns)
                  *** ValueError: Shape of passed values is (1, 1), indices imply (51, 1)
                  

                  ipdb> DataFrame(data[0], columns=columns)
                  *** PandasError: DataFrame constructor not properly called!
                  

                  请帮忙 :) 谢谢!

                  推荐答案

                  从 Pandas 0.12 开始(我相信)你可以做到:

                  As of Pandas 0.12 (I believe) you can do:

                  import pandas
                  import pyodbc
                  
                  sql = 'select * from table'
                  cnn = pyodbc.connect(...)
                  
                  data = pandas.read_sql(sql, cnn)
                  

                  在 0.12 之前,您可以这样做:

                  Prior to 0.12, you could do:

                  import pandas
                  from pandas.io.sql import read_frame
                  import pyodbc
                  
                  sql = 'select * from table'
                  cnn = pyodbc.connect(...)
                  
                  data = read_frame(sql, cnn)
                  

                  这篇关于PYODBC 到 Pandas - DataFrame 不起作用 - 传递值的形状是(x,y),索引暗示(w,z)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:随机选择子目录中的 x 个文件 下一篇:Pandas:如何使用 df.to_dict() 轻松共享示例数据框?

                  相关文章

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

                  <tfoot id='hRLVh'></tfoot>

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