简体   繁体   中英

Append results from multiple SQL Queries into a Pandas Dataframe or Dictionary

I am trying to loop through several SQL Queries and append the results for these queries in a dataframe or dictionary with the key being the SQL Query

I was able to retrieve results from these SQL Queries.

from pandas import DataFrame
for index, row in df.iterrows(): 
    cur.execute(row["SQL_Query"]) 
    print(cur.fetchall())

Output:

[(datetime.date(2019, 4, 8), datetime.date(2019, 4, 1))]

[(2, )]

[('6', 2), ('7', 2)]

[(13, 2)]

But when I try to add them to a dataframe, I am only able to fetch result from the last query.

from pandas import DataFrame
for index, row in df.iterrows(): 
    res = cur.execute(row["SQL_Query"]) 
    df['Results'] = DataFrame(cur.fetchall())

"Output"

The goal right now is only to have results from SQL Queries in different columns. For instance, is the query return Total Count and Failed Count, then that be in 2 different columns.

The issue is that you're iterating over the dataframe for queries and then assigning over df['Results'] each time. You should apply an index value.

The below should work or at least get you on the right track. I can't replicate this quickly to test it.

from pandas import DataFrame
for index, row in df.iterrows(): 
    cur.execute(row["SQL_Query"]) 
    df.loc[index,'Results'] = cur.fetchall()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM