Pandas read_excel numbering Same Column Names in Excel even if its not repeated in UseCols range

Viewed 118

I have an excel Sheet Similar to below example

in excel from A to E

Object Name Col Name Object Name File name
KT Category KT KTOut.csv
KT SubCategory KC KCOut.csv
df = pd.read_excel('map.xlsx', usecols='D:E')

In the df, Object column is coming as Object.1

Since iam not reading the A:B, why pandas is renaming. Can it be avoided? or is there a workaround.

1 Answers

One workaround could be to read the column headers directly, using openpyxl for example, and then rename the columns after read_excel:

import pandas as pd
from openpyxl import load_workbook

wb = load_workbook('map.xlsx')
ws = wb.worksheets[0]
usecols='D:E'
cols = [col[0].value for col in ws[usecols]]

df = pd.read_excel('map.xlsx', usecols=usecols)
df.columns = cols
Related