This question already has answers here:
GroupBy pandas DataFrame and select most common value
                                
                                    (9个答案)
                                
                        
                                在11个月前关闭。
            
                    
数据框:

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


我需要在每个组的MOST_FREQUENT列中获得最频繁的值:

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


我找到的唯一线索-mode(),但不适用于DataFrameGroupBy

编辑:我需要一个解决方案,满足熊猫的.agg()功能

最佳答案

您可以使用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


另一种解决方案是使用SeriesGroupBy.value_counts并返回第一个index值,因为value_counts对值进行排序:

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


编辑:您可以使用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

07-24 09:52