Reputation: 161
How can I use xlwings to read a "table" in excel, into a pandas DataFrame, where the table "headers" become the DataFrame column names?
Every way I have tried to read the table, the header row is always excluded from the read!
Here is what I've tried, where "b" is my xlwings workbook object:
b.sheets['Sheet1'].range('Table1').options(pd.DataFrame)
b.sheets['Sheet1'].range('Table1').options(pd.DataFrame, headers=False)
b.sheets['Sheet1'].range('Table1').options(pd.DataFrame, headers=True)
Upvotes: 4
Views: 6977
Reputation: 1631
Let me add the comment by Felix Zumstein as explicit answer, because in my opinion it is the best solution.
df = b.sheets['Sheet1'].range('Table1[[#All]]').options(pd.DataFrame).value
Using the square bracket notation expands the selection from the table body to the header row as well. Subsequently the conversion to a pandas DataFrame, can use it as header.
To read more, check this answer.
Upvotes: 1
Reputation: 607
Another option is to use the api and Excel's ListObject
import xlwings as xw
wb = xw.books.active
ws = wb.sheets('MySheet')
tbl = ws.api.ListObjects('MyTable') # or .ListObjects(1)
rng = ws.range(tbl.range.address) # get range from table address
df = rng.options(pd.DataFrame, header=True).value # load range to dataframe
Upvotes: 1
Reputation: 161
Hoping this is not the best answer, but I did find I could reference the named range, then .offset(-1).expand('vertical')
Upvotes: 2