Reading Excel file with multiple sheets into individual dataframes within a loop.

Hey,

I'm bashing my head here. I've got a spreadsheet with multiple sheets in it

import pandas as pd
settings_loc = "mysettings.xlsx"
excel_file = pd.read_excel(settings_loc, 
sheet_name=None)
for sheet in excel_file:
    sheet = pd.read_excel(settings_loc,sheet_name=sheet)

The above just puts the last sheet into a dataframe called "sheet" & ignores my sheet names completely. Changing the read_excel line to print returns my sheet names as expected Settings more_settings even_more_settings and_some_more

Anyone able to cast any light on this please & thanks.

edit: I've got it to work. No idea how though, well I understand how the "{}".format() call works, but the locals() I'm not that sure about.

import pandas as pd
settings_loc = "mysettings.xlsx"
excel_file = pd.read_excel(settings_loc, sheet_name=None)
for sheet in excel_file:
    locals()["{}".format(sheet)] = excel_file[sheet]

Posted Dec 6, 2018, 12:58 AM UTC · edited

12
Loading comments…