NumPy has np.percentile().
import numpy as np
a = np.array([1,2,3,4,5])
p = np.percentile(a, 50) # return 50th percentile, i.e. median.
>>> print(p)
3.0
SciPy has scipy.stats.scoreatpercentile(), in addition to many other statistical goodies.
NumPy
numpy.org › devdocs › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v2.5.dev0 Manual
The - 1 in the formulas for j and g accounts for Python’s 0-based indexing. The table above includes only the estimators from H&F that are continuous functions of probability q (estimators 4-9). NumPy also provides the three discontinuous estimators from H&F (estimators 1-3), where j is defined as above, m is defined as follows, and g is a function of the real-valued index = q*n + m - 1 and j. ... For backward compatibility with previous versions of NumPy, quantile ...
Top answer 1 of 12
405
NumPy has np.percentile().
import numpy as np
a = np.array([1,2,3,4,5])
p = np.percentile(a, 50) # return 50th percentile, i.e. median.
>>> print(p)
3.0
SciPy has scipy.stats.scoreatpercentile(), in addition to many other statistical goodies.
2 of 12
95
By the way, there is a pure-Python implementation of percentile function, in case one doesn't want to depend on scipy. The function is copied below:
## {{{ http://code.activestate.com/recipes/511478/ (r1)
import math
import functools
def percentile(N, percent, key=lambda x:x):
"""
Find the percentile of a list of values.
@parameter N - is a list of values. Note N MUST BE already sorted.
@parameter percent - a float value from 0.0 to 1.0.
@parameter key - optional key function to compute value from each element of N.
@return - the percentile of the values
"""
if not N:
return None
k = (len(N)-1) * percent
f = math.floor(k)
c = math.ceil(k)
if f == c:
return key(N[int(k)])
d0 = key(N[int(f)]) * (c-k)
d1 = key(N[int(c)]) * (k-f)
return d0+d1
# median is 50th percentile.
median = functools.partial(percentile, percent=0.5)
## end of http://code.activestate.com/recipes/511478/ }}}
Statistics Globe
statisticsglobe.com › home › python programming language for statistics & data science › quantile in python (4 examples)
Quantile in Python (Example) | Get Quartile of List & DataFrame Column
April 5, 2023 - Now, we can use the quantile function of the NumPy package to create different types of quantiles in Python. The following syntax returns the quartiles of our list object. Note that we are using the arange function within the quantile function to specify the sequence of quantiles to compute.
Educative
educative.io › answers › what-is-the-statistics-quantiles-method-in-python
What is the statistics quantiles() method in Python?
The statistics.quantiles() method returns a list that contains the numeric values of the upper n-1 quantiles.
GeeksforGeeks
geeksforgeeks.org › numpy-quantile-in-python
numpy.quantile() in Python - GeeksforGeeks
April 22, 2025 - numpy.nanquantile(arr, q, axis = None) : Compute the qth quantile of the given data (array elements) along the specified axis, ignoring the nan values. Quantiles plays a very important role in statistics. In the figure given above, Q2 is the median and Q3 - Q1 represents the Interquartile Range of ... In this article, we are going to see how to perform quantile regression in Python.
Programiz
programiz.com › python-programming › numpy › methods › quantile
NumPy quantile()
If the input contains integers or floats smaller than float64, the output data type is float64. Otherwise, the output data type is the same as that of the input. The numpy.quantile() method returns the q-th quantile(s) of the input array along the specified axis.
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.quantile.html
pandas.DataFrame.quantile — pandas 3.0.1 documentation
This optional parameter specifies the interpolation method to use, when the desired quantile lies between two data points i and j: linear: i + (j - i) * fraction, where fraction is the fractional part of the index surrounded by i and j.
Codecademy
codecademy.com › learn › learn-statistics-with-python › modules › quartiles-quantiles-and-interquartile-range › cheatsheet
Learn Statistics with Python: Quartiles, Quantiles, and Interquartile Range Cheatsheet | Codecademy
In Python, the numpy.quantile() function takes an array and a number say q between 0 and 1. It returns the value at the qth quantile. For example, numpy.quantile(data, 0.25) returns the value at the first quartile of the dataset data.
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v2.0 Manual
The optional method parameter specifies ... desired quantile lies between two indexes i and j = i + 1. In that case, we first determine i + g, a virtual index that lies between i and j, where i is the floor and g is the fractional part of the index. The final result is, then, an interpolation of a[i] and a[j] based on g. During the computation of g, i and j are modified using correction constants alpha and beta whose choices depend on the method used. Finally, note that since Python uses 0-based ...
Python Pool
pythonpool.com › home › blog › numpy quantile() explained with examples
Numpy Quantile() Explained With Examples - Python Pool
July 10, 2021 - For better understanding, we looked at a couple of examples. We varied the syntax and looked at the output for each case. In the end, we can conclude that NumPy quantile() helps us in finding the quantile along the specified axis. I hope this article was able to clear all doubts. But in case you have any unsolved queries feel free to write them below in the comment section. Done reading this, why not check how to convert the table to normal form next. ... Python ...
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.Series.quantile.html
pandas.Series.quantile — pandas 3.0.2 documentation
If q is an array, a Series will be returned where the index is q and the values are the quantiles, otherwise a float will be returned. ... Calculate the rolling quantile. ... Returns the q-th percentile(s) of the array elements.
SciPy
docs.scipy.org › doc › scipy › reference › generated › scipy.stats.mstats.mquantiles.html
mquantiles — SciPy v1.17.0 Manual
scipy.stats.mstats.mquantiles(a, prob=(0.25, 0.5, 0.75), alphap=0.4, betap=0.4, axis=None, limit=())[source]# Computes empirical quantiles for a data array.
NumPy
numpy.org › doc › stable › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v2.4 Manual
June 22, 2021 - The - 1 in the formulas for j and g accounts for Python’s 0-based indexing. The table above includes only the estimators from H&F that are continuous functions of probability q (estimators 4-9). NumPy also provides the three discontinuous estimators from H&F (estimators 1-3), where j is defined as above, m is defined as follows, and g is a function of the real-valued index = q*n + m - 1 and j. ... For backward compatibility with previous versions of NumPy, quantile ...
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v2.1 Manual
The - 1 in the formulas for j and g accounts for Python’s 0-based indexing. The table above includes only the estimators from H&F that are continuous functions of probability q (estimators 4-9). NumPy also provides the three discontinuous estimators from H&F (estimators 1-3), where j is defined as above, m is defined as follows, and g is a function of the real-valued index = q*n + m - 1 and j. ... For backward compatibility with previous versions of NumPy, quantile ...
Stack Overflow
stackoverflow.com › questions › 33741545 › python-quantile-for-list-of-sublists
numpy - Python: quantile for list of sublists - Stack Overflow
I want a way to find quantiles (like numpy.percentile) for the 2:nd elements in the sublist [[1,3,1,1],[1,2,0,1],[9,3,2,1]] and in [[1,2,3,4],[0,2,0,0],[1,2,2,2]] and then I want to do a maximum function so I know which subgroup of those two had the highest chosen quantile, and I also want to know the values the other 3 constant values (1:st, 3:rd and 4:th elements) has at that maximum. ... Hi! Welcome to StackOverflow! Take a minute to see the how to ask section! SO users expect questioners to come up with some code to look at... ... Here's one possible way. Assuming (as in your question) List=[[[1,3,0,1],[1,2,0,1],[1,3,0,1]],[[2,2,1,0],[2,2,1,0],[2,2,1,0]]]
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.DataFrame.quantile.html
pandas.DataFrame.quantile — pandas 3.0.2 documentation
Value between 0 <= q <= 1, the quantile(s) to compute. axis{0 or ‘index’, 1 or ‘columns’}, default 0 · Equals 0 or ‘index’ for row-wise, 1 or ‘columns’ for column-wise. ... Include only float, int or boolean data. Changed in version 2.0.0: The default value of numeric_only ...
Interactive Chaos
interactivechaos.com › en › python › function › statisticsquantiles
statistics.quantiles | Interactive Chaos
April 7, 2021 - The statistics.quantiles function returns the quantiles corresponding to the numbers contained in the iterable data. If the parameter n takes the value 4, the function returns the numerical values corresponding to the first, second and third quartiles. If n takes the value 100, the function ...
Top answer 1 of 15
103
By using pandas:
df.time_diff.quantile([0.25,0.5,0.75])
Out[793]:
0.25 0.483333
0.50 0.500000
0.75 0.516667
Name: time_diff, dtype: float64
2 of 15
94
You can use np.percentile to calculate quartiles (including the median):
>>> np.percentile(df.time_diff, 25) # Q1
0.48333300000000001
>>> np.percentile(df.time_diff, 50) # median
0.5
>>> np.percentile(df.time_diff, 75) # Q3
0.51666699999999999
Or all at once:
>>> np.percentile(df.time_diff, [25, 50, 75])
array([ 0.483333, 0.5 , 0.516667])