Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 620
SkillSet12345 Avatar asked Dec 12 '17 09:12

SkillSet12345


1 Answers

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())
like image 120
KPLauritzen Avatar answered Nov 15 '22 16:11

KPLauritzen