95
from pandas import DataFrame import pyodbc cnxn = pyodbc.connect(databasez) cursor.execute("""SELECT ID, NAME AS Nickname, ADDRESS AS Residence FROM tablez""") DF = DataFrame(cursor.fetchall()) 

This is fine to populate my pandas DataFrame. But how do I get

DF.columns = ['ID', 'Nickname', 'Residence'] 

straight from cursor? Is that information stored in cursor at all?

0

4 Answers 4

168

You can get the columns from the cursor description:

columns = [column[0] for column in cursor.description]

Sign up to request clarification or add additional context in comments.

3 Comments

In case anyone is curious what the other values in .description mean: github.com/mkleehammer/pyodbc/wiki/Cursor#attributes
When I tried this, I got a TypeError: 'NoneType' object is not iterable.
A "NoneType" TypeError will occur when the description attribute is None due to no rows being returned, as described here: code.google.com/archive/p/pyodbc/wikis/Cursor.wiki
56

Recent pandas have a higher level read_sql functions that can do this for you

import pyodbc import pandas as pd cnxn = pyodbc.connect(databasez) DF = pd.read_sql_query("SELECT ID, NAME AS Nickname, ADDRESS AS Residence FROM tablez", cnxn) 

Comments

17

In case you are experiencing the NoneType error from the code provided by Matti John, make sure to make the cursor.description call after you have retrieved data from the database. An example:

cursor = cnxn.cursor() cursor.execute("SELECT * FROM my_table") columns = [column[0] for column in cursor.description] 

This fixed it for me.

Comments

6

Improving on the previous answer, in the context of pandas, I found this does exactly what I expect:

DF.columns = DataFrame(np.matrix(cursor.description))[0] 

1 Comment

ValueError: Length mismatch: Expected axis has 1 elements, new values have 7 elements Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.