13

I want to use query() to filter rows in a panda dataframe that appear in a given list. Similar to this question, but I really would prefer to use query()

import pandas as pd
df = pd.DataFrame({'A' : [5,6,3,4], 'B' : [1,2,3, 5]})
mylist =[5,3]

I tried:

df.query('A.isin(mylist)')
0

1 Answer 1

18

You could try this, using @, that allows us to refer a variable in the environment:

df.query('A in @mylist')

Or this:

df.query('A.isin(@mylist)',engine='python')
Sign up to request clarification or add additional context in comments.

Comments

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.