简体   繁体   中英

Filtering dataframe based on variable number of conditions

I have a dataframe like this for example:

df = pd.DataFrame({'A':['a', 'a', 'b', 'c', 'a', 'b',], 'B': [1, 2, 3, 4, 5, 6,]})

What I need is to filter the df based on the value in column 'A'. The problem is that the values to filter by are supplied by the end user. For example:

cond = ['a', 'b']

means that the user wants to filter the df and keep all values 'a' and 'b' in the column 'A'. So in this case I'll need to filter the df with this condition:

df = df.loc[(df['A'] == 'a') | (df['A'] == 'b')]

But the next time the values in the cond list can be different and I need to account for it. So far I've tried the for loop. I was pretty certain it wasn't going to work... and it didn't:

for item in cond:
    df = df.loc[df['A'] == item]

I've also tried to create a generator under df.query() and had high hopes for this, but it didn't work either. Unfortunately, the method doesn't accept generators:

df = df.query(f'A == {x}' for x in cond)
# or
df = df.query('A == @x' for x in cond)

Not quite sure what else to try. Has anyone dealt with this type of problem before?

你可以试试

df = df.loc[df['A'].isin(cond)]

也可以尝试替代@BEN_YO

 df.query('A==@cond')

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