Hi I am trying to figure out how to filter in an Excel table.
import xlsxwriter
import pandas as pd
df = pd.DataFrame({'Region' : ['East', 'West', 'South', 'North'],
'Item' : ['Pear', 'Grape', 'Peach', 'March'],
'Volume' : [9000, 8000, 7000, 6000],
'Month' : ['July', 'March', 'April', 'June']})
writer = pd.ExcelWriter('filter_Test.xlsx', engine = 'xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)
workbook = writer.book
worksheet = writer.sheets['Sheet1']
(max_row, max_col) = df.shape
worksheet.set_column(0, max_col - 1, 12)
worksheet.autofilter(0, 0, max_row, max_col - 1)
worksheet.filter_column(3, 'Month == March')
for row_num in (df.index[(df['Month'] != 'March')].tolist()):
worksheet.set_row(row_num + 1, options={'hidden': True})
This is the result but I want to see in a table:
if I add these lines.
column_settings = [{'header': column} for column in df.columns]
worksheet.add_table(0, 0, max_row, max_col - 1, {'columns': column_settings})
I will get this error:
Any help will be greatly appreciated.