PYODBC to Pandas - DataFrame not working - Shape of passed values is (x,y), indices imply (w,z)

江枫思渺然 提交于 2019-11-30 03:45:07

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)

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)

This is because the cursor returns not a list of tuples but a list of the Row objects, which are similar to tuples, better, actually, but they confuse the pandas dataframe constructor. In the original example, do this before creating the data frame:

for i in range(0,len(temp)):
    temp[i]=tuple(temp[i])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!