计算 Pandas 系列的每个唯一值的频率计数
让我们看看如何找到 Pandas 系列的每个唯一值的频率计数。我们将使用 value_counts()函数来执行此任务。
示例 1:
# importing the module
import pandas as pd
# creating the series
s = pd.Series(data = [2, 3, 4, 5, 5, 6,
7, 8, 9, 5, 3])
# displaying the series
print(s)
# finding the unique count
print(s.value_counts())
输出 :
输出:
示例 2:
# importing the module
import pandas as pd
# creating the series
s = pd.Series(np.take(list('0123456789'),
np.random.randint(10, size = 40)))
# displaying the series
print(s)
# finding the unique count
s.value_counts()
输出 :
输出:
示例 3:
# importing pandas as pd
import pandas as pd
# creating the Series
sr = pd.Series(['Mumbai', 'Pune', 'Agra', 'Pune',
'Goa', 'Shimla', 'Goa', 'Pune'])
# displaying the series
print(sr)
# finding the unique count
sr.value_counts()
输出 :
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。