How to get the nth percentile of a Pandas series?

PythonPandasServer Side ProgrammingProgramming

A percentile is a term used in statistics to express how a score compares to other scores in the same set. In this program, we have to find nth percentile of a Pandas series.

Algorithm

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

Example Code

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))

Output

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

Explanation

The quantile function in the Pandas library takes values only between 0 and 1 as parameters. Therefore, we have to divide the percentile value by 100 before passing it to the quantile function.

raja
Published on 16-Mar-2021 10:51:24
Advertisements