How to filter a table with RoboCorp using the Filter Table By Column function

How to filter a table with RoboCorp using the Filter Table By Column function

The RPA.Tables library allows us to do all sorts of things with tables and knowing how to filter a table is quite helpful. In this short post, I will show you have to filter a table by a column using single and multiple values as filters.

Using one value as a filter

Imagine we have a table that consists of various columns and rows with data. For our imagined case it is a list of people and their professions. We only need to work with Developers and so we need to filter out everyone else. Our .csv file looks like this:

id,firstname,lastname,profession
100,Tybie,Naor,police officer
101,Alex,Cristi,worker
102,Giustina,Madaih,doctor
103,Dennie,Pelagias,doctor
104,Jany,Georas,firefighter
105,Hope,Alabaster,worker
106,Ayn,Jefferey,developer
107,Moyna,Othilia,firefighter
108,Briney,Therine,police officer
109,Fanchon,Brady,worker

In this example, we have simply stored it in the same folder as our bot and named it test_data.csv. To filter out all professions except for Developer, our RoboCorp code should look like this:

*** Settings ***
Documentation       How to filter columns

Library             RPA.Tables


*** Tasks ***
Filter Table Example
    ${table}    Read table from CSV    test_data.csv

    # Filters out all professions in the "profession" column except for Developer
    Filter Table By Column    
    ...    table=${table}    
    ...    column=profession    
    ...    operator=in    
    ...    value=developer
    Write table to CSV    ${table}    filtered.csv

In the file filtered.csv you can see the result

Using multiple values as a filter

What is also quite helpful is knowing how to filter a table using multiple values. Take for instance the case where you would like to filter your table for both Developers and Doctors. In this case, we need to create a list of values:

*** Settings ***
Documentation       How to filter columns

Library             RPA.Tables


*** Tasks ***
Filter Table Example
    ${table}    Read table from CSV    test_data.csv

    # Filters out all professions in the "profession" column except for Developer
    Filter Table By Column    
    ...    table=${table}    
    ...    column=profession    
    ...    operator=in    
    ...    value=['developer','doctor']
    Write table to CSV    ${table}    filtered.csv

The End

I spend some time myself figuring out how to filter a table with multiple values. Eventually, I had to ask in the RoboCorp Slack channel, where helpful people are always ready to give you a helping hand.

I hope this short post was helpful.