Filtering a table based on the output of a variable

Hello all,
The answer to my question might be simple, but I can’t seem to find the solution for some reason…

What I’m trying to do:

I would like to filter a table based on the values contained in a specific column. Normally I would use the expression: app_tables.df_articles_with_topics_nl.search(column_name = 1).

The difficulty in my case is that the column name it should filter is not always the same, but depends on the output of a dropdown variable. In my case, the dropdown variable name is ‘index_selected_technology’, and an output of the variable could be ‘t72’ (which corresponds to one column in the table).
If I write 'print(app_tables.df_articles_with_topics_nl.search(index_selected_technology=1)), it logically returns 'anvil.tables.TableError: No such column ‘index_selected_technology’ in table ‘df_articles_with_topics_nl’.

Is there a way to pass the column name as an output of a variable in the expression above? Or is there another way to do it?

Thanks in advance!

You can build a dictionary where the key name is the column name, and the value is the value you want to match that column against. Similar to the technique shown here: Creating dynamic query - #2 by stefano.menci although that technique shows a more complicated query

1 Like

This works, thank you!

1 Like