By Jamal Hussein · Last updated:
Percentile Calculator h = (n − 1) · p / 100
Paste a list of numbers and choose a percentile p. You get the p-th percentile by linear interpolation (the NumPy default) and by nearest-rank, or the percentile rank of a value, with a dot plot and the working.
A percentile is the value at or below which a given percentage of a data set falls: the 90th percentile is the number that 90% of the observations do not exceed. This percentile calculator takes a list of numbers and returns the p-th percentile by linear interpolation (NumPy’s default) and by nearest-rank, plus the percentile rank of any value you choose.
What is a percentile?
Sort your data from smallest to largest and the p-th percentile is the point that splits it so that roughly p% of the values sit at or below it. The 50th percentile is the median, and the 25th and 75th are the first and third quartiles, which you can get directly from the quartile calculator. The 0th percentile is the minimum and the 100th is the maximum.
The word “roughly” matters. With five numbers there is no value that has exactly 40% of the data below it, so every tool has to adopt a convention for the gaps between observations. That convention is why two correct programs can print two different answers.
How is a percentile calculated?
The linear interpolation method, used by default in NumPy, pandas and Excel’s PERCENTILE.INC, turns p into a fractional position in the sorted data and interpolates between the two values on either side:
Here \(n\) is the number of values, \(p\) is the percentile from 0 to 100, \(x_0 \le x_1 \le \dots \le x_{n-1}\) are the sorted values indexed from zero, \(h\) is the fractional index, and \(\lfloor h \rfloor\) and \(\lceil h \rceil\) are \(h\) rounded down and up. When \(h\) is a whole number the percentile is simply the value at that index.
The nearest-rank method does no interpolation. It computes an ordinal rank, counted from 1, and returns the observation at that rank:
where \(x_{(r)}\) is the r-th smallest value. The result is always a number that actually appears in the data.
Worked example: the 40th percentile of 15, 20, 35, 40, 50
This is the default input in the calculator above. The data is already sorted and \(n = 5\).
Linear interpolation. The fractional index is \(h = (5-1)\times 40/100 = 1.6\). That lies between index 1 (the value 20) and index 2 (the value 35), with a fractional part of 0.6:
Nearest-rank. The rank is \(\lceil 0.40 \times 5 \rceil = \lceil 2 \rceil = 2\), and the 2nd smallest value is 20, so \(P_{40} = 20\).
Same data, same percentile, and the answers are 29 and 20. Neither is wrong. At p = 50 the two methods agree on 35, because \(h = 2\) lands exactly on the middle value and the rank \(\lceil 2.5 \rceil = 3\) points to the same one.
Why do different tools give different percentiles?
A sample of n values only pins down n points of the distribution; everything in between is a modelling choice. Hyndman and Fan catalogued nine definitions in 1996, and NumPy exposes them, plus four simpler variants, through the method argument of numpy.percentile. The table shows what each returns for the 40th percentile of 15, 20, 35, 40, 50.
NumPy method | How it picks the value | Same rule as | P40 |
|---|---|---|---|
linear | Interpolate at h = (n − 1)p/100 | NumPy, pandas, Excel PERCENTILE.INC, R type 7 | 29 |
inverted_cdf | Nearest-rank: value at rank ⌈np/100⌉ | R type 1 | 20 |
lower | Lower neighbour of h, no interpolation | – | 20 |
higher | Upper neighbour of h, no interpolation | – | 35 |
nearest | Neighbour whose index is closest to h | – | 35 |
midpoint | Average of the two neighbours | – | 27.5 |
weibull | Interpolate at position (n + 1)p/100 | Excel PERCENTILE.EXC, R type 6 | 26 |
hazen | Interpolate at position np/100 + 0.5 | R type 5 | 27.5 |
median_unbiased | Interpolate at (n + 1/3)p/100 + 1/3 | R type 8 | 27 |
Nine rules give six distinct answers, from 20 to 35, on a five-point sample. The spread shrinks quickly as n grows: with thousands of observations the neighbours are close together and every method lands within a hair of the others. The disagreement is a small-sample problem, so always state the method when you report a percentile from a handful of values.
What is the difference between a percentile and a percentile rank?
They are inverse questions. A percentile starts from a percentage and returns a value (“what latency do 95% of requests stay under?”). A percentile rank starts from a value and returns a percentage (“what share of requests finished in 200 ms or less?”). The second mode of the calculator uses the at-or-below definition:
For 35 in the example data, three of the five values (15, 20 and 35) are less than or equal to 35, so its percentile rank is \(3/5 \times 100 = 60\).
Where are percentiles used in machine learning?
- Latency and serving metrics. Model endpoints are judged on p95 and p99 latency, not the mean, because a few slow requests dominate user experience while barely moving an average. A service with a 40 ms mean and an 800 ms p99 has a tail problem the mean hides.
- Clipping outliers. Winsorizing a feature at its 1st and 99th percentiles caps extreme values without dropping rows, which stabilises models that are sensitive to scale such as a least-squares regression line. The cut-offs must be computed on the training split only and then reused on validation and test data.
- Quantile features. Quantile binning and scikit-learn’s
QuantileTransformerreplace raw values with their percentile rank, giving a uniform, outlier-resistant feature. - Threshold selection. Flagging the top 5% of anomaly scores means thresholding at the 95th percentile; the precision and recall that follow can be checked with a confusion matrix.
- Quantile regression and intervals. Predicting the 10th and 90th percentiles of a target gives a prediction interval, a different summary from the mean that an expected value describes.
Common mistakes with percentiles
- Passing a fraction where a percent is expected.
np.percentile(a, 0.95)is the 0.95th percentile, almost the minimum. Usenp.percentile(a, 95)ornp.quantile(a, 0.95). - Comparing numbers from different methods. Excel’s
PERCENTILE.EXC, a SQLPERCENTILE_DISCand NumPy’s default can all disagree on small samples. - Averaging percentiles. The mean of per-server p99 values is not the fleet p99. Percentiles have to be recomputed from the pooled data or from mergeable sketches.
- Reading a tail percentile from too little data. With 20 observations the p99 by linear interpolation falls between the two largest values, so it reflects one or two observations rather than the shape of the tail.
- Confusing percentile with percentage. Scoring in the 80th percentile means beating about 80% of the group, not getting 80% of the answers right.
Percentiles in Python with NumPy and SciPy
This reproduces the worked example and the percentile rank:
import numpy as np
from scipy import stats
data = [15, 20, 35, 40, 50]
print(np.percentile(data, 40)) # 29.0 (linear, the default)
print(np.percentile(data, 40, method="inverted_cdf")) # 20 (nearest-rank)
print(np.percentile(data, [0, 50, 100])) # [15. 35. 50.]
print(stats.percentileofscore(data, 35, kind="weak")) # 60.0 (percentile rank)
The method keyword needs NumPy 1.22 or later; older versions call it interpolation and offer only the five simple variants. The full list of options is in the numpy.percentile documentation, and the rank definitions (weak, strict, mean, rank) are described in the scipy.stats.percentileofscore documentation.
Frequently asked questions
Which percentile method should I use?
Use linear interpolation unless you have a reason not to: it is the default in NumPy, pandas, R and Excel, so your numbers will match what most people expect. Use nearest-rank when the result must be an actual observation, for example a real request latency or an integer count.
Is the 50th percentile the same as the median?
Yes. With linear interpolation the 50th percentile equals the usual median: the middle value for an odd count, and the average of the two middle values for an even count. Nearest-rank returns the lower of the two middle values when the count is even.
Why does Excel give a different percentile than NumPy?
Excel’s PERCENTILE and PERCENTILE.INC use the same linear rule as NumPy and match it. PERCENTILE.EXC uses the position (n + 1)p/100 instead, which NumPy calls the weibull method, so it returns 26 rather than 29 for the 40th percentile of 15, 20, 35, 40, 50.
What are the 0th and 100th percentiles?
With linear interpolation the 0th percentile is the minimum and the 100th percentile is the maximum of the data. For 15, 20, 35, 40, 50 they are 15 and 50. Nearest-rank gives the same two values because the rank is clamped to at least 1.
Related calculators
Percentiles are one of several summaries of a sample. Browse the rest in the statistics calculators hub, or go straight to Q1, Q2, Q3 and the interquartile range with the quartiles and IQR tool.