pandas read_excel(sheet name = None) 返回一个字符串字典,而不是数据框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47768950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas read_excel(sheet name = None) returns a dictionary of strings, not dataframes?
提问by SkillSet12345
The pandas read_excel documentationsays that specifying sheet_name = Noneshould return "All sheets as a dictionary of DataFrames". However when I try to use it like so I get a dictionary of strings.
该大Pandasread_excel文件说,指定SHEET_NAME =无应返回“所有纸张作为DataFrames的字典”。但是,当我尝试像这样使用它时,我得到了一个字符串字典。
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.
我不明白为什么后者返回字符串。我希望能够将每个工作表作为数据框访问,在该工作表(数据框)上执行字符串替换,然后使用 to_excel 将这些工作表放入新的 xlsx 文件中。但我很困惑为什么sheet in data:正在返回字符串。
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)
采纳答案by KPLauritzen
data = pd.read_excel(target_file, sheet_name = None)
returns a Dict
.
When you loop over data
you get the keys of the Dict
.
data = pd.read_excel(target_file, sheet_name = None)
返回一个Dict
. 当你循环时,data
你会得到Dict
.
Try:
尝试:
for key in data:
print(data[key].head())