Issue
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.
Solution
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
Answered By - BigBen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.