[英]select groups having more than x members
在熊貓中,是否有一種方法可以從分組的數據框中選擇具有x個以上成員的組?
就像是:
grouped = df.groupby(['a', 'b'])
dupes = [g[['a', 'b', 'c', 'd']] for _, g in grouped if len(g) > 1]
我在文檔或SO中找不到解決方案。
使用filter
:
grouped.filter(lambda x: len(x) > 1)
例:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.