pandas 有没有办法从分组数据框中选择具有超过 x 个成员的组?
就像是:
grouped = df.groupby(['a', 'b'])
dupes = [g[['a', 'b', 'c', 'd']] for _, g in grouped if len(g) > 1]
我在文档或 SO 上找不到解决方案。
pandas 有没有办法从分组数据框中选择具有超过 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