简体   繁体   中英

pandas groupby filter nunique

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?

I will recommend

gb= df.groupby(['date'])
df = df[gb['barcode'].transform('nunqiue').eq(1)]

nunique is a method, not a property. Fix:

gb.filter(lambda x: x['barcode'].nunique() ==1)

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