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
You can insert a list of values into a cell in Pandas DataFrame using DataFrame.at() , DataFrame. iat() , and DataFrame.
Pandas DataFrame query() Method The query() method allows you to query the DataFrame. The query() method takes a query expression as a string parameter, which has to evaluate to either True of False. It returns the DataFrame where the result is True according to the query expression.
Creating a list of Dataframes. To create a list of Dataframes we use the list() function in R and then pass each of the data frame you have created as arguments to the function.
Use @
to reference variables in query
:
filter_list=['Foo','Bar']
df.query("Category == @filter_list")
Output:
Index Category
0 1 Foo
1 2 Bar
3 4 Foo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With