简体   繁体   中英

How can I append results from a loop of SQL queries to a dataframe

I have a list of values created from some analysis I did on Pandas. I then want to run a for loop to execute a SQL query using each of the values in that list.

I'd like these results to either be appended to a new DataFrame- I can initialise previously, or to add each one to a new DataFrame, which I can then append myself.

Example code is below, where df['a'] would be the results column of my previous analysis.

df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
columns=['a', 'b', 'c'])
x = df['a']
r = pd.DataFrame(columns=['F', 'G', 'H', 'I'])
for m in x:
    r.append(pd.read_sql_query("""SELECT a.F,a.G,a.H,a.I
                                    FROM users a
                                    WHERE a.F ="""+m,engine))

I'd like all the the data to append to r

You should execute a single query instead of iterating over x , like this:

SELECT a.F,a.G,a.H,a.I FROM users a WHERE a.F in (1, 4, 7)

The code below will do that:

import pandas as pd
import numpy as np

df = pd.DataFrame(
    np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
    columns=['a', 'b', 'c']
)

x = df['a']
x_list = ', '.join(str(i) for i in x)

r = pd.DataFrame(columns=['F', 'G', 'H', 'I'])
query = f'SELECT a.F,a.G,a.H,a.I FROM users a WHERE a.F in ({x_list})'
r.append(pd.read_sql_query(query, engine))

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