Reputation: 7806
So far an example of what I have is here:
df = pd.DataFrame({"barcode": [1,2,2,3,3,4, 4, 4], "date": ['today', 'today', 'tomorrow', 'tomorrow', 'tomorrow', 'yesterday', 'yesterday' ,'yesterday'], "info": [40,20,10,15,17,19, 21, 23]})
gb= df.groupby(['date'])
gb.filter(lambda x: x['barcode'].nunique!=1)
which returns:
Empty DataFrame
Columns: [barcode, date, info]
Index: []
Only "yesterday" should remain after I filter this because there are 2 distinct barcodes in the group "today", and 2 distinct barcodes in the group "tomorrow". What is going on here? and in the example the column to filter on is sorted but does it need to be?
Upvotes: 0
Views: 551
Reputation: 323306
I will recommend
gb= df.groupby(['date'])
df = df[gb['barcode'].transform('nunqiue').eq(1)]
Upvotes: 2
Reputation: 15738
nunique
is a method, not a property. Fix:
gb.filter(lambda x: x['barcode'].nunique() ==1)
Upvotes: 1