Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Loading data from a stored proc with multiple results sets into a Pandas DataFrame
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('mysql://{}:{}@{}/{}'.format(username, password, server, database_name))
connection = engine.connect().connection
cursor = connection.cursor()
cursor.execute('call storedProcName(%s, %s, ...)', params)
# Result set 1
column_names = [col[0] for col in cursor.description] # Get column names from MySQL
df1_data = []
for row in cursor.fetchall():
df1_data.append({name: row[i] for i, name in enumerate(column_names)})
# Result set 2
cursor.nextset()
column_names = [col[0] for col in cursor.description] # Get column names from MySQL
df2_data = []
for row in cursor.fetchall():
df2_data.append({name: row[j] for j, name in enumerate(column_names)})
cursor.close()
df1 = pd.DataFrame(df1_data)
df2 = pd.DataFrame(df2_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.