pandas: filter group by multiple conditions?

filter, multiple-conditions, pandas, python

Solution

d1 = df.set_index(['id', 'date']).is_local.unstack()
d1.index[d1['2016-01-01'] & ~d1['2017-01-01']].tolist()

[123]

Problem

I have a data frame that looks like this: ``` df = pd.DataFrame([ {'id': 123, 'date': '2016-01-01', 'is_local': True }, {'id': 123, 'date': '2017-01-01', 'is_local': False }, {'id': 124, 'date': '2016-01-01', 'is_local': True }, {'id': 124, 'date': '2017-01-01', 'is_local': True } ]) df.date = df.date.astype('datetime64[ns]') ``` I want to get a list of all the IDs for which `is_local` was True at the start of 2016, but False at the start of 2017. I've started by grouping by ID: ``` gp = df.groupby('id') ``` Then I've tried this just to filter by the second of these conditions (as a way of getting started), but it's returning all the groups: ``` gp.apply(lambda x: ~x.is_local & (x.date > '2016-12-31')) ``` How can I filter in the way I need?

Original source