24

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

3 Answers 3

38

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
Sign up to request clarification or add additional context in comments.

Comments

23

Use isin method.

df.query('Category.isin(@filter_list)')

1 Comment

I find this syntax much more clear than the accepted answer (for which I would expect exact list equality). Works for me in Pandas version 1.2.4.
10

Try this:

df = pd.DataFrame({'Index':[1,2,3,4],'Category':['Foo','Bar','Cho','Foo']})
filter_list = ['Foo','Bar']

df.query(f'Category=={filter_list}')

2 Comments

You can also write df.query(f'Category in {filter_list}'), which may be more readable
df.query("Category in @filter_list") works as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.