shoggananna
shoggananna

Reputation: 565

usecols issue in read_excel: passing in a string list

I have generated a list of the alphabet (every 2nd letter) to be passed into usecols as part of reading in an excel sheet.

Input:

 l=list(string.ascii_uppercase)
 l=l[1::2]
 df=pd.read_excel(ExcelName,sheet_name='test',usecols=l)

Output:list, error

['B', 'D', 'F', 'H', 'J', 'L', 'N', 'P', 'R', 'T', 'V', 'X', 'Z']

ValueError: Usecols do not match columns, columns expected but not found: ['V', 'H', 'L', 'P', 'Z', 'T', 'R', 'J', 'F', 'N', 'B', 'D', 'X']

I have also tried lowercase...not sure whats happening

Upvotes: 0

Views: 307

Answers (1)

divingTobi
divingTobi

Reputation: 2300

The usecols parameter of read_excel accepts numbers, not letters. So your code should be

l = list(range(2, 14, 2))
df = pd.read_excel(ExcelName, sheet_name='test', usecol=l)

See read_excel for details.

Upvotes: 1

Related Questions