数据帧值频率

时间:2021-03-25 12:53:57

标签: python pandas dataframe

我有一个 DataFrame,我想在整个框架中找到值频率。

    a   b
0   5   7
1   7   8
2   5   7

结果应该是这样的:

 5 2
 7 3
 8 1

2 个答案:

答案 0 :(得分:1)

DataFrame.stackSeries.value_countsSeries.sort_index 一起使用:

s = df.stack().value_counts().sort_index()

DataFrame.melt

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
相关问题