Find most frequent observation in group [duplicate

2020-04-20 19:52发布

DataFrame:

B = pd.DataFrame({'b':['II','II','II','II','II','I','I','I'],
                  'MOST_FREQUENT':['1', '2', '2', '1', '1','1','2','2']})

I need to get the most frequent value in a column MOST_FREQUENT for each group:

pd.DataFrame({'b':['I','II'],
                      'MOST_FREQUENT':['2','1']})

The only clue i found - mode(), but is not applieble to DataFrameGroupBy

EDIT: I need a solution, which satisfies the pandas' .agg() function

2条回答
ゆ 、 Hurt°
2楼-- · 2020-04-20 20:15

Trying to squeeze a little more performance out of pandas, we can use groupby with size to get the counts. then use idxmax to find the index values of the largest sub-groups. These indices will be the values we're looking for.

s = B.groupby(['MOST_FREQUENT', 'b']).size()
pd.DataFrame(
    s.groupby(level='b').idxmax().values.tolist(),
    columns=s.index.names
)

  MOST_FREQUENT   b
0             2   I
1             1  II

naive timing

enter image description here

查看更多
家丑人穷心不美
3楼-- · 2020-04-20 20:28

You can use apply:

print (B.groupby('b')['MOST_FREQUENT'].apply(lambda x: x.mode())
        .reset_index(level=1, drop=True).reset_index())
    b MOST_FREQUENT
0   I             2
1  II             1

Another solution is use SeriesGroupBy.value_counts and return first index value, because value_counts sorts values:

print (B.groupby('b')['MOST_FREQUENT'].apply(lambda x: x.value_counts().index[0])
        .reset_index())
    b MOST_FREQUENT
0   I             2
1  II             1

EDIT: You can use most_common

from collections import Counter
print (B.groupby(['b']).agg(lambda x: Counter(x).most_common(1)[0][0]).reset_index())
    b MOST_FREQUENT
0   I             2
1  II             1
查看更多
登录 后发表回答