python - pandas - parse excel columns from start column until empty column

Viewed 49

Hey I am trying to parse an excel using pandas. I want to parse 1 excel row (e.g first row) starting from a fixed column in the excel (e.g. column "F") and until the last non-empty column.

Until now the last non-empty column was "M" so I used a fixed number for the end like this:

pd.read_excel(excel_file,"header": None,"nrows": 1, usecols="F:M")

- A B C D E F G H I J K L M
1 col_A col_B col_C col_D col_E FIRST_COL col_G col_H col_I col_J col_K col_L LAST_COL

The numbers of columns can grow to the right, and I still want to parse the added columns. The starting column is fixed though.

I tried to look in panda's usecols documentation Also I saw that you can use usecols with names, but I don't have any knowledge about the name of the last column or any column in this excel. I can modify the excel though, e.g. to sign the with a special value the column after the last one to parse (if needed), but I don't know how to parse the excel even with that addition.

pandas==1.3.4
Python 3.7.5
Windows 10
Excel 2019
1 Answers

As suggested here I solved it by slicing the parsed excel right after reading it.

HEADER_START_COLUMN = str.lower("F")
ASCII_OF_A_LETTER = 96
HEADER_START_COLUMN_INT = ord(HEADER_START_COLUMN) - ASCII_OF_A_LETTER
pd_excel = pd.read_excel(excel_file,"header": None,"nrows": 1).iloc[:, HEADER_START_COLUMN_INT:]
Related