将 SQL 查询结果转换为 Pandas 数据结构
问题:
用户需要帮助将 SQL 查询结果转换为 Pandas 数据结构。用户尝试打印查询结果,但没有获得任何有用的信息。
解决方案:
要将 SQL 查询结果转换为 Pandas,可以采取以下步骤:
示例代码:
from sqlalchemy import create_engine from pandas import DataFrame engine2 = create_engine('mysql://THE DATABASE I AM ACCESSING') connection2 = engine2.connect() dataid = 1022 resoverall = connection2.execute("SELECT sum(BLABLA) AS BLA, sum(BLABLABLA2) AS BLABLABLA2, sum(SOME_INT) AS SOME_INT, sum(SOME_INT2) AS SOME_INT2, 100*sum(SOME_INT2)/sum(SOME_INT) AS ctr, sum(SOME_INT2)/sum(SOME_INT) AS cpc FROM daily_report_cooked WHERE campaign_id = '%s'", %dataid) df = DataFrame(resoverall.fetchall()) df.columns = resoverall.keys()
以上是如何高效地将 SQL 查询结果转换为 Pandas DataFrame?的详细内容。更多信息请关注PHP中文网其他相关文章!