Imagine we want part of a dataset based on specific columns, which is on a worksheet by OpenPyXL.
ws = openpyxl.load_workbook("example_file.xlsx")["example_sheet"]
Is there a simple way to subset recurring to the tuples, such like the following ranges (Excel-like columns) are pretended?
ws = ws[ws['A,C,D,J:K,AI:AK']]
I know Pandas allows solving this, but the previous library is necessary to deal with multi-level columns for instance. Even so, iterations seem to be the unique way but not clear enough. Thanks in advance!
No, what you are looking for is not possible. You need to iterate through the rows for row in ws.iter_rows()
and then iterate through the cells for cell in row
and filter the columns that you are looking for and extract the values.
Consider the following implementation, it has not been tested, improvise it for your need.
for row in ws.iter_rows():
for cell in row:
if cell.column_letter in ['A,C,D,J,K,O,R']:
value.append(cell.Value)