Skip to content

Instantly share code, notes, and snippets.

@masnick
Last active July 14, 2017 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masnick/cfd311497879d2f71a9b99fe87907bb0 to your computer and use it in GitHub Desktop.
Save masnick/cfd311497879d2f71a9b99fe87907bb0 to your computer and use it in GitHub Desktop.
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