pandas read_excel(sheet name = None) returns a dictionary of strings, not dataframes?

SkillSet12345 picture SkillSet12345 · Dec 12, 2017 · Viewed 7k times · Source

The pandas read_excel documentation says that specifying sheet_name = None should return "All sheets as a dictionary of DataFrames". However when I try to use it like so I get a dictionary of strings.

target_file = "C:\file_here.xlsx"
data = pd.read_excel(target_file) 
print(type(data))
data = pd.read_excel(target_file, sheet_name = None)
print(type(data))
#print(data)
for sheet in data:
    print(type(sheet))

This returns:

<class 'pandas.core.frame.DataFrame'>
<class 'collections.OrderedDict'>
<class 'str'>

I don't understand why the latter returns strings. I want to be able to access each sheet as a dataframe, perform a string replace on that sheet(dataframe), and then put those sheets to a new xlsx file using to_excel. But I'm confused why for sheet in data: is returning strings.

If I print data (the ordered dictionary) below in the following snippet, it prints the dataframes to console. So it looks like I'm not accessing the dictionary correctly, but I'd like to understand what I'm doing wrong exactly:

data = pd.read_excel(target_file, sheet_name = None)
print(type(data))
print(data)

Answer

KPLauritzen picture KPLauritzen · Dec 12, 2017

data = pd.read_excel(target_file, sheet_name = None) returns a Dict. When you loop over data you get the keys of the Dict.

Try:

for key in data:
     print(data[key].head())