我有一个 DataFrame,我想在整个框架中找到值频率。
a b
0 5 7
1 7 8
2 5 7
结果应该是这样的:
5 2
7 3
8 1
答案 0 :(得分:1)
将 DataFrame.stack
与 Series.value_counts
和 Series.sort_index
一起使用:
s = df.stack().value_counts().sort_index()
s = df.melt()['value'].value_counts().sort_index()
print (s)
5 2
7 3
8 1
Name: value, dtype: int64
答案 1 :(得分:0)
一种简单的方法是使用 pd.Series
来查找唯一计数:
import pandas as pd
# creating the series
s = pd.Series(data = [5,10,9,8,8,4,5,9,10,0,1])
# finding the unique count
print(s.value_counts())
输出:
10 2
9 2
8 2
5 2
4 1
1 1
0 1