0

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

  • Can one assume that the data always begins in column A? – BigBen Mar 28 '22 at 15:53
  • 1
    Does this answer your question? [Convert spreadsheet number to column letter](https://stackoverflow.com/questions/23861680/convert-spreadsheet-number-to-column-letter) – Warcupine Mar 28 '22 at 15:56
  • im sorry, let´s assume data will NOT always be in the same column – oscar salgado Mar 28 '22 at 15:59
  • If the data doesn't always begin in column A, then AFAIK there is no way to do this in pandas. I'd use openpyxl or xlsxwriter. – BigBen Mar 28 '22 at 16:04

1 Answers1

0

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
Corralien
  • 109,409
  • 8
  • 28
  • 52