【发布时间】:2015-03-18 14:49:50
【问题描述】:
所以我连接到数据库并使用 fetchall() 来提取我的数据
cursor = connection.cursor()
sql = """
SELECT name, n
from table
"""
cursor.execute(sql)
rows = cursor.fetchall(
)
我的行导致元组的元组我正在尝试将其用于具有列名的数据框
我尝试在下面使用,但是
df = pd.DataFrame(rows, columns=['name', 'n'])
但出现错误:DataFrame 构造函数未正确调用!
行 ((u'a', 1.0), (u'b', 2.0), (u'c', 3.0))
值来自 unicode ,但我只希望我的数据框如下所示,任何建议都将不胜感激。
【问题讨论】:
-
直接从 SQL 加载 df 可能性能更高:pandas.pydata.org/pandas-docs/stable/io.html#io-sql