'Append result from SQL query to column in Pandas df

I have a dataframe (test_df) that looks like this:

dq_code      dq_sql                    SQL_Output
ID_24        Select * from table1
ID_42        Select * from table2

I want to execute each SQL query from the SQL column and add the result to the SQL_Output column. I tried the following:

#Create engine with sqlalchemy
engine = create_engine(ENGINE_PATH)

#Load data to test_df by initial query
test_df = pd.read_sql_query('Select dq_code, dq_sql from random_table', engine)

#Add empty column to df
test_df['SQL_Output'] = ''

#Additional clean-ups
test_df['SQL_Output'] = test_df['SQL_Output'].replace(np.nan, '')
test_df = test_df.replace(r'\n',' ', regex=True)
test_df['dq_sql'] = test_df['dq_sql'].apply(str)

for row in test_df:
    test_df['SQL_Output'] = test_df.append(pd.read_sql(test_df['dq_sql'], engine))

But got the following error:

AttributeError: 'Series' object has no attribute '_execute_on_connection'

Not sure how to fix this error. Any help would be appreciated!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source