I am performing a query on a DataFrame:
Index Category
1 Foo
2 Bar
3 Cho
4 Foo
I would like to return the rows where the category is "Foo" or "Bar". When I use the code:
df.query("Catergory==['Foo','Bar']")
This works fine and returns:
Index Category
1 Foo
2 Bar
4 Foo
However in future I will want the filter to be changed dynamically so I wrote:
filter_list=['Foo','Bar']
df.query("Catergory==filter_list")
Which threw out the error:
UndefinedVariableError: name 'filter_list' is not defined
Other variations I tried with no success were:
df.query("Catergory"==filter_list)
df.query("Catergory=="filter_list)
Respectively producing:
ValueError: expr must be a string to be evaluated, <class 'bool'> given
SyntaxError: invalid syntax