我有熊猫数据框中的数据,我使用 Python 将其存储在 SQLITE 数据库中。当我尝试查询其中的表时,我可以获得结果但没有列名。有人可以指导我。
sql_query = """Select date(report_date), insertion_order_id, sum(impressions), sum(clicks), (sum(clicks)+0.0)/sum(impressions)*100 as CTR
from RawDailySummaries
Group By report_date, insertion_order_id
Having report_date like '2014-08-12%' """
cursor.execute(sql_query)
query1 = cursor.fetchall()
for i in query1:
print i
下面是我得到的输出
(u'2014-08-12', 10187, 2024, 8, 0.3952569169960474)
(u'2014-08-12', 12419, 15054, 176, 1.1691244851866613)
我需要做什么才能以带有列名的表格形式显示结果
原文由 bshah 发布,翻译遵循 CC BY-SA 4.0 许可协议
第 1 步:选择您的引擎,例如 pyodbc、SQLAlchemy 等。
第二步:建立连接
cursor = connection.cursor()
第三步:执行SQL语句
cursor.execute("Select * from db.table where condition=1")
第 4 步:从连接变量描述中提取 Header