Login
📚 Python for Introductory Statistics
Chapters ▾

7.2 The SciPy Library

SciPy (pronounced “Sigh Pie”) is a Python-based ecosystem of open-source software library for mathematics, science, and engineering. In addition, stats is a module in scipy library given as scipy.stats.

The scipy.stats module This module from SciPy contains a large number of probability distributions as well as a growing library of statistical functions. We will use it extensively in our probability and inferential statistics sections later.

stats syntax for percentile rank

from scipy import stats 
stats.percentileofscore(x, data_value, kind='rank')

Example: The dataset below is scores of 20 students in a stats class. Find the percentile rank of the score 50.

81, 21, 53, 32, 80, 13, 26, 42, 35, 41, 14, 25, 87, 62, 46, 40, 32, 52, 85, 28, 17, 36, 83, 79, 18, 50, 17, 36, 36, 19

Watch demo video

from scipy import stats 

x = [81, 21, 53, 32, 80, 13, 26, 42, 35, 41, 14, 25, 87, 62, 46, 40, 
             32, 52, 85, 28, 17, 36, 83, 79, 18, 50, 17, 36, 36, 19]

stats.percentileofscore(x,50,kind='rank')
Show expected output
70.0

Interpretation: A student with an exam score of 50 is in the 70th percentile. This student's score is at or better than 70 percent of the students. Or, 70% of the scores are at or below 50.

Example: Below is the list of ages of US Senators (2020). Write a program to compute the percentile age rank of a Senator aged 50. Watch demo video

81,81,80,80,80,80,78,78,78,77,77,75,75,74,74,74,73,72,72,71,71,71,71,70,70, 70,70,68,68,68,68,68,67,67,67,66,66,65,65,65,65,64,64,64,64,64,64,63,63, 63,63,62,62,62,62,62,61,61,61,60,60,59,59,59,59,59,59,59,59,58,58,57,57, 56,56,55,54,54,54,54,54,53,53,52,52,52,52,52,51,50,49,48,46,45,44,43,43, 43,42,41

import numpy as np
from scipy import stats

age = [81,81,80,80,80,80,78,78,78,77,77,75,75,74,74,74,73,72,72,71,71,71,71,70,70,
        70,70,68,68,68,68,68,67,67,67,66,66,65,65,65,65,64,64,64,64,64,64,63,63,
        63,63,62,62,62,62,62,61,61,61,60,60,59,59,59,59,59,59,59,59,58,58,57,57,
        56,56,55,54,54,54,54,54,53,53,52,52,52,52,52,51,50,49,48,46,45,44,43,43,
        43,42,41]

age_array = np.array(age)   # convert the list to a numpy array

stats.percentileofscore(age_array,50,kind='rank')
Show expected output
11.0

Interpretation: The percentile age rank of a Senator aged 50 is 11.

Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.