Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีหา Percentile ที่ n ของ Pandas Series


Percentile เป็นศัพท์ในสถิติใช้แสดงว่าคะแนนหนึ่งเปรียบเทียบกับคะแนนอื่นในชุดเดียวกันอย่างไร ในโปรแกรมนี้ เราต้องหา Percentile ที่ n ของ Pandas Series

อัลกอริทึม

ขั้นตอนที่ 1: กำหนด Pandas Series
ขั้นตอนที่ 2: รับค่า Percentile จากผู้ใช้
ขั้นตอนที่ 3: คำนวณ Percentile
ขั้นตอนที่ 4: แสดง 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

คำอธิบาย

ฟังก์ชัน quantile ในไลบรารี Pandas รับพารามิเตอร์ได้เฉพาะค่าระหว่าง 0 และ 1 เท่านั้น ดังนั้นเราต้องหารค่า Percentile ด้วย 100 ก่อนส่งให้ฟังก์ชัน quantile