Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 587
Night Walker Avatar asked Jul 31 '16 07:07

Night Walker


1 Answers

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)))]
like image 156
Nickil Maveli Avatar answered Oct 19 '22 21:10

Nickil Maveli