How do I calculate percentiles with python/numpy?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In NumPy, percentiles are calculated with numpy.percentile, which returns the value below which a chosen percentage of the data falls. The function is simple to use for one-dimensional arrays, but it also supports multiple percentiles, axis-aware calculations, and different estimation methods.
The Basic Call
For a one-dimensional array, the syntax is straightforward:
This calculates:
- the median when
q=50 - the 90th percentile when
q=90
The percentile argument must be in the range from 0 to 100.
Request Several Percentiles at Once
NumPy can return multiple percentiles in one call:
This is useful when you want quartiles or a standard summary of distribution spread.
Common interpretations:
- 25th percentile is the first quartile
- 50th percentile is the median
- 75th percentile is the third quartile
Calculate Percentiles Along an Axis
For multidimensional arrays, use the axis parameter to control the direction of the calculation.
Here:
- '
axis=0computes percentiles column by column' - '
axis=1computes percentiles row by row'
If you omit axis, NumPy flattens the array first.
Be Aware of the Estimation Method
Percentiles are not always defined identically across tools, especially for small datasets. NumPy exposes this through the method parameter.
Different methods can produce different answers on the same data. That matters when:
- you need to match another system
- you are comparing results against Excel, R, or a database
- the dataset is small and interpolation choices matter
In modern NumPy, the argument is named method. Older code and older documentation may still mention interpolation, which has been replaced.
Handling Missing Values
If your array contains NaN values, regular percentile will propagate them. Use nanpercentile when you want to ignore missing entries.
This is a common source of confusion in data analysis workflows where missing values are normal.
Percentiles and Quantiles Are Closely Related
NumPy also provides quantile, which is the same idea expressed on a 0 to 1 scale instead of 0 to 100.
These two calls refer to the same location in the distribution.
Keep the Use Case in Mind
Percentiles are especially useful for:
- latency distributions
- income or salary summaries
- exam score cutoffs
- monitoring systems where the median and tail matter more than the mean
For example, the 95th percentile of request latency is often more informative than the average because it captures slow-tail behavior directly.
Common Pitfalls
- Passing percentile values on a
0to1scale tonp.percentile, which expects0to100. - Forgetting about the
axisargument and accidentally computing percentiles over the flattened array. - Comparing results with another tool without matching the percentile estimation method.
- Using
np.percentileon arrays withNaNvalues whennp.nanpercentileis the intended function. - Treating percentiles as unique universal values even though small-sample interpolation choices can differ across implementations.
Summary
- Use
np.percentile(data, q)to compute theqth percentile of an array. - Pass a list of percentile values when you want several results in one call.
- Use
axisto control whether the calculation happens across rows, columns, or the flattened array. - Use
methodwhen you need to control how NumPy estimates percentiles. - Use
np.nanpercentileif missing values should be ignored rather than propagated.

