简体   繁体   中英

select groups having more than x members

Is there a way in pandas to select, out of a grouped dataframe, the groups with more than x members ?

something like:

grouped = df.groupby(['a', 'b'])
dupes = [g[['a', 'b', 'c', 'd']] for _, g in grouped if len(g) > 1]

I can't find a solution in the docs or on SO.

use filter :

grouped.filter(lambda x: len(x) > 1)

Example:

In [64]:
df = pd.DataFrame({'a':[0,0,1,2],'b':np.arange(4)})
df

Out[64]:
   a  b
0  0  0
1  0  1
2  1  2
3  2  3

In [65]:
df.groupby('a').filter(lambda x: len(x)>1)

Out[65]:
   a  b
0  0  0
1  0  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