pandasExcelFile.parse()读取文件在dict而不是数据框

我是新来的python,甚至更新的pandas,但相对精通R.我使用的是python,与Python 3.5和pandas0.18.1。 我想读取一个excel文件作为一个数据框。 该文件承认是相当…丑陋。 有很多空的空间,缺less标题等(我不知道这是否是任何问题的根源)

我创build文件对象,然后find适当的工作表,然后尝试读取该表作为数据框:

xl = pd.ExcelFile(allFiles[i]) sName = [s for s in xl.sheet_names if 'security exposure' in s.lower()] df = xl.parse(sName) df 

结果:

 {'Security exposure - 21 day lag': Percent of Total Holdings \ 0 KMNFC vs. 3 Month LIBOR AUD 1 04-OCT-16 2 Australian Dollar 3 NaN 4 NaN 5 NaN 6 NaN 7 NaN 8 Long/Short Net Exposure 9 Total 10 NaN 11 Long 12 NaN 13 NaN 14 NaN 15 NaN 16 NaN 17 NaN 

(这继续20-30多行和5-6多列)

我正在使用Anaconda和Spyder,它有一个“可变资源pipe理器”。 它显示variablesdf是DataFrametypes的字典:

在这里输入图像说明

但是,我不能使用iloc:

 df.iloc[:,1] Traceback (most recent call last): File "<ipython-input-77-d7b3e16ccc56>", line 1, in <module> df.iloc[:,1] AttributeError: 'dict' object has no attribute 'iloc' 

有什么想法吗? 我错过了什么?

编辑:

要清楚的是,我真正想要做的是参考df的第一列。 在R中这将是df [,1]。 环顾四周,似乎不是一种非常stream行的做事方式,或者不是“正确的”方式。 我明白为什么通过列名称或键索引更好,但在这种情况下,我真的只需要按列号索引dataframe。 任何这样做的工作方法将不胜感激。

编辑(2):

根据build议,我尝试了“read_excel”,结果如下:

 df = pd.ExcelFile(allFiles[i]).parse(sName) df.loc[1] Traceback (most recent call last): File "<ipython-input-90-fc40aa59bd20>", line 2, in <module> df.loc[1] AttributeError: 'dict' object has no attribute 'loc' df = pd.read_excel(allFiles[i], sheetname = sName) df.loc[1] Traceback (most recent call last): File "<ipython-input-91-72b8405c6c42>", line 2, in <module> df.loc[1] AttributeError: 'dict' object has no attribute 'loc' 

问题在这里:

 sName = [s for s in xl.sheet_names if 'security exposure' in s.lower()] 

它返回了一个单一的元素列表。 我将其更改为以下内容:

 sName = [s for s in xl.sheet_names if 'security exposure' in s.lower()][0] 

它返回一个string,然后代码按预期执行。

所有这一切都要感谢阿瀚指出这一点。