如何獲得 Pandas 系列的第 n 個百分位數?


百分位數是統計學中用於表示分數與同一組中的其他分數的比較情況的術語。在此程式中,我們必須查詢 Pandas 系列的第 n 個百分位數。

演算法

Step 1: Define a Pandas series.
Step 2: Input percentile value.
Step 3: Calculate the percentile.
Step 4: Print the percentile.

示例程式碼

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 庫中的 quantile 函式的引數取值為 0 到 1 之間的值。因此,在將百分位數傳遞給 quantile 函式之前,我們必須將百分位數值除以 100。

更新於: 2021 年 3 月 16 日

1K+ 瀏覽量

開啟您的 職業 生涯

完成課程獲得認證

開始試用
廣告
© . All rights reserved.