Last active
July 14, 2017 15:45
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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