Home > Software design >  obtain header "letter" searching for column name in excel with pandas and python
obtain header "letter" searching for column name in excel with pandas and python

Time:03-29

im using pandas with excel and i would like to get the letter of the header in excel searching for column name.

here´s an example

1

i would like to do something LIKE this: df.columns.get_loc("SR Status") and i would like to return: "D"

i have already done this:

import pandas
df = pd.read_excel("file.xls")
df.columns.get_loc("SR Status")

and let´s assume data will NOT always be in the same place. sometimes it might be at header "A" but other time could be on other place thanks in advance

CodePudding user response:

You can use get_column_letter:

import pandas as pd
from openpyxl.utils import get_column_letter

df = pd.read_excel('data.xlsx', usecols='D:F')
offset = 4  # D

col = get_column_letter(df.columns.get_loc('SR Status')   offset)
print(col)  # Output: D
  • Related