Python XlsxWriter – Applying Filter


Python XlsxWriter – Applying Filter



”;


In Excel, you can set filter on a tabular data based upon criteria using logical expressions. In XlsxWriter”s worksheet class, we have autofilter() method or the purpose. The mandatory argument to this method is the cell range. This creates drop-down selectors in the heading row. To apply some criteria, we have two methods available − filter_column() or filter_column_list().

Applying Filter Criteria for a Column

In the following example, the data in the range A1:D51 (i.e. cells 0,0 to 50,3) is used as the range argument for autofilter() method. The filter criteria ”Region == East” is set on 0th column (with Region heading) with filter_column() method.

Example

All the rows in the data range not meeting the filter criteria are hidden by setting hidden option to true for the set_row() method of the worksheet object.


import xlsxwriter

wb = xlsxwriter.Workbook(''hello.xlsx'')
ws = wb.add_worksheet()

data = (
   [''Region'', ''SalesRep'', ''Product'', ''Units''],
   [''East'',   ''Tom'',      ''Apple'',    6380],
   [''West'',   ''Fred'',     ''Grape'',    5619],
   [''North'',  ''Amy'',      ''Pear'',     4565],
   [''South'',  ''Sal'',      ''Banana'',   5323],
   [''East'',   ''Fritz'',    ''Apple'',    4394],
   [''West'',   ''Sravan'',   ''Grape'',    7195],
   [''North'',  ''Xi'',       ''Pear'',     5231],
   [''South'',  ''Hector'',   ''Banana'',   2427],
   [''East'',   ''Tom'',      ''Banana'',   4213],
   [''West'',   ''Fred'',     ''Pear'',     3239],
   [''North'',  ''Amy'',      ''Grape'',    6520],
   [''South'',  ''Sal'',      ''Apple'',    1310],
   [''East'',   ''Fritz'',    ''Banana'',   6274],
   [''West'',   ''Sravan'',   ''Pear'',     4894],
   [''North'',  ''Xi'',       ''Grape'',    7580],
   [''South'',  ''Hector'',   ''Apple'',    9814]
)
for row in range(len(data)):
   ws.write_row(row,0, data[row])
   
ws.autofilter(0, 0, 50, 3)

ws.filter_column(0, ''Region == East'')

row = 1
for row_data in (data):
   region = row_data[0]
   
   if region != ''East'':
      ws.set_row(row, options={''hidden'': True})
      
   ws.write_row(row, 0, row_data)
   
   row += 1
wb.close()

Output

When we open the worksheet with the help of Excel, we will find that only the rows with Region=”East” are visible and others are hidden (which you can display again by clearing the filter).


Filter Criteria1

The column parameter can either be a zero indexed column number or a string column name. All the logical operators allowed in Python can be used in criteria (==, !=, , =). Filter criteria can be defined on more than one columns and they can be combined by and or or operators. An example of criteria with logical operator can be as follows −


ws.filter_column(''A'', ''x > 2000'')
ws.filter_column(''A'', ''x != 2000'')
ws.filter_column(''A'', ''x > 2000 and x

Note that "x" in the criteria argument is just a formal place holder and can be any suitable string as it is ignored anyway internally.


ws.filter_column(''A'', ''price > 2000'')
ws.filter_column(''A'', ''x != 2000'')
ws.filter_column(''A'', ''marks > 60 and x<75'')

XlsxWriter also allows the use of wild cards "*" and "?" in the filter criteria on columns containing string data.


ws.filter_column(''A'', name=K*'') #starts with K
ws.filter_column(''A'', name=*K*'') #contains K
ws.filter_column(''A'', name=?K*'') # second character as K
ws.filter_column(''A'', name=*K??'') #any two characters after K

Example

In the following example, first filter on column A requires region to be West and second filter''s criteria on column D is "units > 5000". Rows not satisfying the condition "region = West" or "units > 5000" are hidden.


import xlsxwriter

wb = xlsxwriter.Workbook(''hello.xlsx'')
ws = wb.add_worksheet()

data = (
   [''Region'',  ''SalesRep'', ''Product'', ''Units''],
   [''East'',    ''Tom'',      ''Apple'',    6380],
   [''West'',    ''Fred'',     ''Grape'',    5619],
   [''North'',   ''Amy'',      ''Pear'',     4565],
   [''South'',   ''Sal'',      ''Banana'',   5323],
   [''East'',    ''Fritz'',    ''Apple'',    4394],
   [''West'',    ''Sravan'',   ''Grape'',    7195],
   [''North'',   ''Xi'',       ''Pear'',     5231],
   [''South'',   ''Hector'',   ''Banana'',   2427],
   [''East'',    ''Tom'',      ''Banana'',   4213],
   [''West'',    ''Fred'',     ''Pear'',     3239],
   [''North'',   ''Amy'',      ''Grape'',    6520],
   [''South'',   ''Sal'',      ''Apple'',    1310],
   [''East'',    ''Fritz'',    ''Banana'',   6274],
   [''West'',    ''Sravan'',   ''Pear'',     4894],
   [''North'',   ''Xi'',       ''Grape'',    7580],
   [''South'',   ''Hector'',   ''Apple'',    9814])
        
for row in range(len(data)):
   ws.write_row(row,0, data[row])
   
ws.autofilter(0, 0, 50, 3)

ws.filter_column(''A'', ''x == West'')
ws.filter_column(''D'', ''x > 5000'')

row = 1
for row_data in (data[1:]):
   region = row_data[0]
   volume = int(row_data[3])
   
   if region == ''West'' or volume > 5000:
      pass
   else:
      ws.set_row(row, options={''hidden'': True})
      
   ws.write_row(row, 0, row_data)
   row += 1
   
wb.close()

Output

In Excel, the filter icon can be seen on columns A and D headings. The filtered data is seen as below −


Filter Criteria2

Applying a Column List Filter

The filter_column_list() method can be used to represent filters with multiple selected criteria in Excel 2007 style.


ws.filter_column_list(col,list)

The second argument is a list of values against which the data in a given column is matched. For example −


ws.filter_column_list(''C'', [''March'', ''April'', ''May''])

It results in filtering the data so that value in column C matches with any item in the list.

Example

In the following example, the filter_column_list() method is used to filter the rows with region equaling either East or West.


import xlsxwriter
wb = xlsxwriter.Workbook(''hello.xlsx'')
ws = wb.add_worksheet()
data = (
   [''Region'', ''SalesRep'', ''Product'', ''Units''],
   [''East'',   ''Tom'',      ''Apple'',    6380],
   [''West'',   ''Fred'',     ''Grape'',    5619],
   [''North'',  ''Amy'',      ''Pear'',     4565],
   [''South'',  ''Sal'',      ''Banana'',   5323],
   [''East'',   ''Fritz'',    ''Apple'',    4394],
   [''West'',   ''Sravan'',   ''Grape'',    7195],
   [''North'',  ''Xi'',       ''Pear'',     5231],
   [''South'',  ''Hector'',   ''Banana'',   2427],
   [''East'',   ''Tom'',      ''Banana'',   4213],
   [''West'',   ''Fred'',     ''Pear'',     3239],
   [''North'',  ''Amy'',      ''Grape'',    6520],
   [''South'',  ''Sal'',      ''Apple'',    1310],
   [''East'',   ''Fritz'',    ''Banana'',   6274],
   [''West'',   ''Sravan'',   ''Pear'',     4894],
   [''North'',  ''Xi'',       ''Grape'',    7580],
   [''South'',  ''Hector'',   ''Apple'',    9814]
)
for row in range(len(data)):
   ws.write_row(row,0, data[row])
   
ws.autofilter(0, 0, 50, 3)

l1= [''East'', ''West'']
ws.filter_column_list(''A'', l1)

row = 1
for row_data in (data[1:]):
   region = row_data[0]
   
   if region not in l1:
      ws.set_row(row, options={''hidden'': True})
      
   ws.write_row(row, 0, row_data)
   row += 1
   
wb.close()

Output

The Column A shows that the autofilter is applied. All the rows with Region as East or West are displayed and rest are hidden.


Column List Filter1

From the Excel software, click on the filter selector arrow in the Region heading and we should see that the filter on region equal to East or West is applied.


Column List Filter2

Advertisements

'';

Leave a Reply

Your email address will not be published. Required fields are marked *