本文介绍了从pyodbc execute()语句返回列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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())
这很好,可以填充我的pandas DataFrame.但是我怎么得到
This is fine to populate my pandas DataFrame. But how do I get
DF.columns = ['ID', 'Nickname', 'Residence']
直接来自光标?这些信息是否完全存储在 cursor 中?
straight from cursor? Is that information stored in cursor at all?
推荐答案
您可以从游标描述中获取列:
You can get the columns from the cursor description:
columns = [column[0] for column in cursor.description]
这篇关于从pyodbc execute()语句返回列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!