You can do this using rank, where pct=True option displays ranks in percentile form.
In [1551]: v = pd.Series([0,2,4,2,10,8,6,1])
In [1556]: v.rank(pct=True)
Out[1556]:
0 0.1250
1 0.4375
2 0.6250
3 0.4375
4 1.0000
5 0.8750
6 0.7500
7 0.2500
dtype: float64
Answer from Mayank Porwal on Stack OverflowPandas
pandas.pydata.org › docs › reference › api › pandas.Series.quantile.html
pandas.Series.quantile — pandas 3.0.6 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.
11:24
Understanding Quantiles in Python: A Step-by-Step Guide (Numpy ...
06:22
Quantile in Python (4 Examples) | Calculate Quartile, Decile & ...
11:08
DataFrame Percentile Quartiles using Numpy, Pandas, & Python - YouTube
PROGRAM#12 | HOW TO SLICE SERIES IN PYTHON | HOW TO CALCULATE ...
02:52
Quantile for a numerical feature using pandas - YouTube
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.Series.quantile.html
pandas.Series.quantile — pandas 3.0.5 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.
TutorialsPoint
tutorialspoint.com › article › how-to-get-the-nth-percentile-of-a-pandas-series
How to get the nth percentile of a Pandas series?
March 25, 2026 - Use series.quantile(q) to find the nth percentile of a Pandas series.
Medium
medium.com › @amit25173 › understanding-percentiles-in-pandas-369166d19e76
Understanding Percentiles in Pandas | by Amit Yadav | Medium
March 6, 2025 - If you’re dealing with percentiles in Python, NumPy’s percentile() is your go-to tool. It’s fast, simple, and perfect for quick calculations. ... import numpy as np import pandas as pd # Sample data data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] # Finding the 90th percentile percentile_90 = np.percentile(data, 90) print("90th Percentile:", percentile_90)
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.describe.html
pandas.Series.describe — pandas 3.0.5 documentation
By default the lower percentile is 25 and the upper percentile is 75.
Pandas
pandas.pydata.org › pandas-docs › version › 0.23 › generated › pandas.Series.quantile.html
pandas.Series.quantile — pandas 0.23.1 documentation
Extending Pandas · Release Notes · Enter search terms or a module, class or function name. Series.quantile(q=0.5, interpolation='linear')[source]¶ · Return value at the given quantile, a la numpy.percentile. See also · pandas.core.window.Rolling.quantile ·
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.Series.describe.html
pandas.Series.describe — pandas 2.3.3 documentation
By default the lower percentile is 25 and the upper percentile is 75.
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.quantile.html
pandas.DataFrame.quantile — pandas 3.0.6 documentation
Numpy function to compute the percentile.
Top answer 1 of 6
183
- You can use the
pandas.DataFrame.quantile()function.- If you look at the API for
quantile(), you will see it takes an argument for how to do interpolation. If you want a quantile that falls between two positions in your data:- 'linear', 'lower', 'higher', 'midpoint', or 'nearest'.
- By default, it performs linear interpolation.
- These interpolation methods are discussed in the Wikipedia article for percentile
- If you look at the API for
import pandas as pd
import numpy as np
# sample data
np.random.seed(2023) # for reproducibility
data = {'Category': np.random.choice(['hot', 'cold'], size=(10,)),
'field_A': np.random.randint(0, 100, size=(10,)),
'field_B': np.random.randint(0, 100, size=(10,))}
df = pd.DataFrame(data)
df.field_A.mean() # Same as df['field_A'].mean()
# 51.1
df.field_A.median()
# 50.0
# You can call `quantile(i)` to get the i'th quantile,
# where `i` should be a fractional number.
df.field_A.quantile(0.1) # 10th percentile
# 15.6
df.field_A.quantile(0.5) # same as median
# 50.0
df.field_A.quantile(0.9) # 90th percentile
# 88.8
df.groupby('Category').field_A.quantile(0.1)
#Category
#cold 28.8
#hot 8.6
#Name: field_A, dtype: float64
df
Category field_A field_B
0 cold 96 58
1 cold 22 28
2 hot 17 81
3 cold 53 71
4 cold 47 63
5 hot 77 48
6 cold 39 32
7 hot 69 29
8 hot 88 49
9 hot 3 49
2 of 6
39
assume series s
s = pd.Series(np.arange(100))
Get quantiles for [.1, .2, .3, .4, .5, .6, .7, .8, .9]
s.quantile(np.linspace(.1, 1, 9, 0))
0.1 9.9
0.2 19.8
0.3 29.7
0.4 39.6
0.5 49.5
0.6 59.4
0.7 69.3
0.8 79.2
0.9 89.1
dtype: float64
OR
s.quantile(np.linspace(.1, 1, 9, 0), 'lower')
0.1 9
0.2 19
0.3 29
0.4 39
0.5 49
0.6 59
0.7 69
0.8 79
0.9 89
dtype: int32