如何计算 Pandas 系列中每个项目的频率?
pythonpandasserver side programmingprogramming更新于 2024/9/22 3:56:17
在此程序中,我们将计算 Pandas 系列中每个元素的频率。pandas 库中的 value_counts() 函数可帮助我们找到元素的频率。
算法
步骤 1:定义 Pandas 系列。 步骤 2:使用 value_counts() 函数打印每个项目的频率。
示例代码
import pandas as pd
series = pd.Series([10,10,20,30,40,30,50,10,60,50,50])
print("Series:\n", series)
frequency = series.value_counts()
print("\n元素频率:\n", frequency)
输出
Series: 0 10 1 10 2 20 3 30 4 40 5 30 6 50 7 10 8 60 9 50 10 50 dtype: int64 元素频率: 50 3 10 3 30 2 20 1 40 1 60 1 dtype: int64
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

