Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an error importing Excel file into pandas selecting the usecols parameter

I'm trying to import data into pandas from an Excel file, but I'm getting an error when typing out the following:

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

But I'm getting an error stating that 'C' is not in list. When evaluating the Excel file in Excel, it clearly has a C column. The pandas documentation says the following:

usecols : int or list, default None

If None then parse all columns, If int then indicates last column to be parsed. If list of ints then indicates list of column numbers to be parsed. If string then indicates comma separated list of Excel column letters and column ranges (e.g. “A:E” or “A,C,E:F”). Ranges are inclusive of both sides.

So I'd like to import just C to F, so I've tried both suggestions as mentioned above.

I get the following error:

ValueError: 'C' is not in list

Not sure why this won't work. Any suggestions?

like image 622
nk abram Avatar asked Jan 11 '18 03:01

nk abram


1 Answers

Have a look at version you're using. If this version is older than version 0.21.0 then try to use parse_cols instead.

columns = 'A:L'
df = pd.read_excel(file_to_process, sheetname=sheetname, parse_cols=columns)

I had the same issue with usecols. After changing to parse_cols it works properly.

like image 78
Sergey Solod Avatar answered Nov 15 '22 08:11

Sergey Solod