简体   繁体   中英

python pandas query for values in list

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)')

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM