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

好久不见. 提交于 2020-12-03 16:57:41

问题


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)

回答1:


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())


来源:https://stackoverflow.com/questions/47768950/pandas-read-excelsheet-name-none-returns-a-dictionary-of-strings-not-datafr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!