我正在尝试找出在数据帧中找到n个最常见的值的最佳方法。我不关心它们所在的行或列,我只想在数据帧中找到最常见的值。
Input:
df = pd.DataFrame({ 'A':list('abcdef'), 'B':[4,5,4,5,5,4], 'C':[7,8,9,4,2,3], 'D':[1,3,5,7,1,0], 'E':[5,3,6,9,2,4], 'F':list('aaabbb') })
Desired output:
'a' 4
'4' 4
'1' 3
'b' 2
...
'8' 1
发布于 2020-07-07 19:36:32
在Series.mode
中使用DataFrame.stack
most = df.stack().mode()
如果需要第一个顶值,则获取第一个值:
most = df.stack().mode().iat[0]
使用Series.value_counts
编辑计数值
count = df.stack().value_counts()
print (count)
5 5
4 5
a 4
b 4
3 3
9 2
7 2
2 2
1 2
f 1
e 1
d 1
c 1
8 1
6 1
0 1
dtype: int64
https://stackoverflow.com/questions/62774373
复制相似问题