Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using lists in a pandas query

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
like image 277
William Wade Avatar asked Aug 28 '19 15:08

William Wade


People also ask

Can you put a list in a Pandas DataFrame?

You can insert a list of values into a cell in Pandas DataFrame using DataFrame.at() , DataFrame. iat() , and DataFrame.

How do you query in pandas?

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.

Can we use list in data frame?

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.


1 Answers

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
like image 88
Scott Boston Avatar answered Oct 12 '22 23:10

Scott Boston