【发布时间】:2021-03-25 21:06:45
【问题描述】:
我有一个 DataFrame,我想在整个帧中查找值频率。
a b
0 5 7
1 7 8
2 5 7
结果应该是这样的:
5 2
7 3
8 1
【问题讨论】:
我有一个 DataFrame,我想在整个帧中查找值频率。
a b
0 5 7
1 7 8
2 5 7
结果应该是这样的:
5 2
7 3
8 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
【讨论】:
一种简单的方法是使用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
【讨论】: