5

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

1 回答 1

4

data = pd.read_excel(target_file, sheet_name = None)返回一个Dict。当你循环过去时,data你会得到Dict.

尝试:

for key in data:
     print(data[key].head())
于 2017-12-12T09:22:51.387 回答