将Excel文件导入pandas并选择usecols参数时出错

nk *_*ram 5 python excel dataframe pandas

我正在尝试从Excel文件中将数据导入到pandas中,但在键入以下内容时出现错误:

energy = pd.read_excel('Indicators.xls',
                       'Energy', 
                       skiprows=17, 
                       skip_footer=38, 
                       usecols=['C','D','E','F'])
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误,说明'C'不在列表中.在Excel中评估Excel文件时,它显然有一C列.pandas文档说明如下:

usecols:int或list,默认为None

如果None则解析所有列,If int则指示要解析的最后一列.如果int列表则表示要解析的列号列表.如果字符串则表示以逗号分隔的Excel列字母和列范围列表(例如"A:E"或"A,C,E:F").范围包括双方.

所以我想只导入CF,所以我都试过建议如上所述.

我收到以下错误:

ValueError: 'C' is not in list
Run Code Online (Sandbox Code Playgroud)

不知道为什么这不起作用.有什么建议?

Ser*_*lod 6

查看您使用的版本。如果此版本早于0.21.0,请尝试使用parse_cols。

columns = 'A:L'
df = pd.read_excel(file_to_process, sheetname=sheetname, parse_cols=columns)
Run Code Online (Sandbox Code Playgroud)

我对usecols有相同的问题。更改为parse_cols后,它可以正常工作。


小智 0

这对我来说效果很好:

dataset=pd.read_excel('testfile.xlsx',usecols="C:F")
Run Code Online (Sandbox Code Playgroud)

输入:

A  B  C  D  E  F G
1  1  1  1  1  1 1
Run Code Online (Sandbox Code Playgroud)

输出:

C D E F
1 1 1 1
Run Code Online (Sandbox Code Playgroud)