⚙️ Setup¶
import numpy as np
import pandas as pd
data = pd.Series([55, 60, 62, 68, 70, 72, 75, 80, 85, 90])
print("Dataset:", list(data))
Dataset: [55, 60, 62, 68, 70, 72, 75, 80, 85, 90]
x = 70
rank = (data <= x).mean() * 100
print(f"Percentile rank of {x} = {rank:.0f}%")
Percentile rank of 70 = 50%
Answer: 5 of the 10 values (55, 60, 62, 68, 70) are ≤ 70, so the percentile rank is 50%. About half the scores are at or below 70.
p80 = np.percentile(data, 80)
print(f"80th percentile = {p80:.1f}")
80th percentile = 81.0
Answer: The 80th percentile is about 84 (NumPy's linear method interpolates between 80 and 85). Roughly 80% of scores fall at or below it.
q1, q2, q3 = np.percentile(data, [25, 50, 75])
d3 = np.percentile(data, 30)
print(f"Q1 = {q1:.1f}, Median (Q2) = {q2:.1f}, Q3 = {q3:.1f}")
print(f"3rd decile (30th percentile) = {d3:.1f}")
Q1 = 63.5, Median (Q2) = 71.0, Q3 = 78.8 3rd decile (30th percentile) = 66.2
Answer: Q1 ≈ 64.5, median = 71, Q3 ≈ 78.75, and the 3rd decile ≈ 66.6. Quartiles and deciles are just percentiles at the 25/50/75 and 10/20/...90 marks.
msg = ("About 30% of children that age are the same height or shorter, ",
"and about 70% are taller. It is NOT a grade or a score of 30%. ",
"A single percentile is not \"good\" or \"bad\"; clinicians watch the trend over time.")
print("".join(msg))
About 30% of children that age are the same height or shorter, and about 70% are taller. It is NOT a grade or a score of 30%. A single percentile is not "good" or "bad"; clinicians watch the trend over time.
Answer: ~30% of same-age children are that height or shorter (≈70% are taller). It is a ranking, not a 30% grade, and a stable 30th-percentile child is perfectly healthy. The trend across visits matters more than any one number.
small = np.array([3, 6, 9, 12])
print(f"method=lower -> {np.percentile(small, 25, method='lower'):.2f}")
print(f"method=linear -> {np.percentile(small, 25, method='linear'):.2f}")
method=lower -> 3.00 method=linear -> 5.25
Answer: "lower" returns 3 (it drops to the nearest data value below), while "linear" interpolates to 5.25 (three-quarters of the way from 3 to 6). The 25% mark falls between two data points, so each method resolves it differently. Neither is wrong, but always report the method you used.