如何获取 Pandas 系列的第 n 个百分位数?
pythonpandasserver side programmingprogramming更新于 2024/9/22 4:28:17
百分位数是统计学中的一个术语,用来表示一个分数与同一组中其他分数的比较情况。在这个程序中,我们必须找到 Pandas 系列的第 n 个百分位数。
算法
步骤 1:定义 Pandas 系列。 步骤 2:输入百分位数值。 步骤 3:计算百分位数。 步骤 4:打印百分位数。
示例代码
import pandas as pd
series = pd.Series([10,20,30,40,50])
print("Series:\n", series)
n = int(input("Enter the percentile you want to calculate: "))
n = n/100
percentile = series.quantile(n)
print("The {} percentile of the given series is: {}".format(n*100, percentile))
输出
Series: 0 10 1 20 2 30 3 40 4 50 dtype: int64 Enter the percentile you want to calculate: 50 The 50.0 percentile of the given series is: 30.0
解释
Pandas 库中的分位数函数仅接受 0 到 1 之间的值作为参数。因此,我们必须将百分位数值除以 100,然后再将其传递给分位数函数。
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

