Night Walker
Night Walker

Reputation: 21280

pandas not condition with filtering

How I can implement not condition on the filtering

grouped = store_ids_with_visits.groupby(level=[0, 1, 2])
grouped.filter(lambda x: (len(x) == 1 and x['template_fk'] == exterior_template))

I want to get all entries that not answering on the condition

I tried doing:

grouped.filter(lambda x: ~(len(x) == 1 and x['template_fk'] == exterior_template))

But got following error:

filter function returned a int, but expected a scalar bool

Upvotes: 14

Views: 32534

Answers (1)

Nickil Maveli
Nickil Maveli

Reputation: 29711

IIUC, you can use isin to check for bool conditions and take only the NOT(~) values of the grouped dataframe:

 df[~df.isin(grouped.filter(lambda x: (len(x) == 1 and x['template_fk'] == exterior_template)))]

Upvotes: 23

Related Questions