TL; DR

Use

sz = temp['INCOME'].size-1
temp['PCNT_LIN'] = temp['INCOME'].rank(method='max').apply(lambda x: 100.0*(x-1)/sz)

   INCOME    PCNT_LIN
0      78   44.444444
1      38   11.111111
2      42   22.222222
3      48   33.333333
4      31    0.000000
5      89   55.555556
6      94   66.666667
7     102   77.777778
8     122  100.000000
9     122  100.000000

Answer

It is actually very simple, once your understand the mechanics. When you are looking for percentile of a score, you already have the scores in each row. The only step left is understanding that you need percentile of numbers that are less or equal to the selected value. This is exactly what parameters kind='weak' of scipy.stats.percentileofscore() and method='average' of DataFrame.rank() do. In order to invert it, run Series.quantile() with interpolation='lower'.

So, the behavior of the scipy.stats.percentileofscore(), Series.rank() and Series.quantile() is consistent, see below:

In[]:
temp = pd.DataFrame([  78, 38, 42, 48, 31, 89, 94, 102, 122, 122], columns=['INCOME'])
temp['PCNT_RANK']=temp['INCOME'].rank(method='max', pct=True)
temp['POF']  = temp['INCOME'].apply(lambda x: scipy.stats.percentileofscore(temp['INCOME'], x, kind='weak'))
temp['QUANTILE_VALUE'] = temp['PCNT_RANK'].apply(lambda x: temp['INCOME'].quantile(x, 'lower'))
temp['RANK']=temp['INCOME'].rank(method='max')
sz = temp['RANK'].size - 1 
temp['PCNT_LIN'] = temp['RANK'].apply(lambda x: (x-1)/sz)
temp['CHK'] = temp['PCNT_LIN'].apply(lambda x: temp['INCOME'].quantile(x))

temp

Out[]:
   INCOME  PCNT_RANK    POF  QUANTILE_VALUE  RANK  PCNT_LIN    CHK
0      78        0.5   50.0              78   5.0  0.444444   78.0
1      38        0.2   20.0              38   2.0  0.111111   38.0
2      42        0.3   30.0              42   3.0  0.222222   42.0
3      48        0.4   40.0              48   4.0  0.333333   48.0
4      31        0.1   10.0              31   1.0  0.000000   31.0
5      89        0.6   60.0              89   6.0  0.555556   89.0
6      94        0.7   70.0              94   7.0  0.666667   94.0
7     102        0.8   80.0             102   8.0  0.777778  102.0
8     122        1.0  100.0             122  10.0  1.000000  122.0
9     122        1.0  100.0             122  10.0  1.000000  122.0

Now in a column PCNT_RANK you get ratio of values that are smaller or equal to the one in a column INCOME. But if you want the "interpolated" ratio, it is in column PCNT_LIN. And as you use Series.rank() for calculations, it is pretty fast and will crunch you 255M numbers in seconds.


Here I will explain how you get the value from using quantile() with linear interpolation:

temp['INCOME'].quantile(0.11)
37.93

Our data temp['INCOME'] has only ten values. According to the formula from your link to Wiki the rank of 11th percentile is

rank = 11*(10-1)/100 + 1 = 1.99

The truncated part of the rank is 1, which corresponds to the value 31, and the value with the rank 2 (i.e. next bin) is 38. The value of fraction is the fractional part of the rank. This leads to the result:

 31 + (38-31)*(0.99) = 37.93

For the values themselves, the fraction part have to be zero, so it is very easy to do the inverse calculation to get percentile:

p = (rank - 1)*100/(10 - 1)

I hope I made it more clear.

Answer from igrinis on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › calculate-arbitrary-percentile-on-pandas-groupby
Calculate Arbitrary Percentile on Pandas GroupBy - GeeksforGeeks
July 23, 2025 - The 90th percentile tells you the value below which 90% of the data lies. In Pandas, we can easily compute percentiles using the quantile() function. It can also be calculated using the NumPy percentile() function.
Discussions

python - How do I get the percentile for a row in a pandas dataframe? - Stack Overflow
Example DataFrame Values - 0 78 1 38 2 42 3 48 4 31 5 89 6 94 7 102 8 122 9 122 stats.percentileofscore(temp['INCOME'].values, 38, kind='mean') 15.0 stats. More on stackoverflow.com
🌐 stackoverflow.com
How to interprete percentile information from the describe function in Pandas? - Data Science Stack Exchange
I am a bit stumped on how to interpret the percentile information you see when you call the describe function on dataframes in Pandas. I believe I have a basic understanding of what percentile mean... More on datascience.stackexchange.com
🌐 datascience.stackexchange.com
October 7, 2020
python - Find percentile stats of a given column - Stack Overflow
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... More on stackoverflow.com
🌐 stackoverflow.com
Percentile range output across multiple columns in python/pandas
df.groupby("type").agg("median") will get you the 50th percentile for "Hello" and "OK". I can't remember off the top of my head, but you can pass a list of aggregations to agg (such as mean, count, min) to get multiple columns as well as use custom functions. More on reddit.com
🌐 r/learnpython
2
3
February 6, 2021
Top answer
1 of 3
48

TL; DR

Use

sz = temp['INCOME'].size-1
temp['PCNT_LIN'] = temp['INCOME'].rank(method='max').apply(lambda x: 100.0*(x-1)/sz)

   INCOME    PCNT_LIN
0      78   44.444444
1      38   11.111111
2      42   22.222222
3      48   33.333333
4      31    0.000000
5      89   55.555556
6      94   66.666667
7     102   77.777778
8     122  100.000000
9     122  100.000000

Answer

It is actually very simple, once your understand the mechanics. When you are looking for percentile of a score, you already have the scores in each row. The only step left is understanding that you need percentile of numbers that are less or equal to the selected value. This is exactly what parameters kind='weak' of scipy.stats.percentileofscore() and method='average' of DataFrame.rank() do. In order to invert it, run Series.quantile() with interpolation='lower'.

So, the behavior of the scipy.stats.percentileofscore(), Series.rank() and Series.quantile() is consistent, see below:

In[]:
temp = pd.DataFrame([  78, 38, 42, 48, 31, 89, 94, 102, 122, 122], columns=['INCOME'])
temp['PCNT_RANK']=temp['INCOME'].rank(method='max', pct=True)
temp['POF']  = temp['INCOME'].apply(lambda x: scipy.stats.percentileofscore(temp['INCOME'], x, kind='weak'))
temp['QUANTILE_VALUE'] = temp['PCNT_RANK'].apply(lambda x: temp['INCOME'].quantile(x, 'lower'))
temp['RANK']=temp['INCOME'].rank(method='max')
sz = temp['RANK'].size - 1 
temp['PCNT_LIN'] = temp['RANK'].apply(lambda x: (x-1)/sz)
temp['CHK'] = temp['PCNT_LIN'].apply(lambda x: temp['INCOME'].quantile(x))

temp

Out[]:
   INCOME  PCNT_RANK    POF  QUANTILE_VALUE  RANK  PCNT_LIN    CHK
0      78        0.5   50.0              78   5.0  0.444444   78.0
1      38        0.2   20.0              38   2.0  0.111111   38.0
2      42        0.3   30.0              42   3.0  0.222222   42.0
3      48        0.4   40.0              48   4.0  0.333333   48.0
4      31        0.1   10.0              31   1.0  0.000000   31.0
5      89        0.6   60.0              89   6.0  0.555556   89.0
6      94        0.7   70.0              94   7.0  0.666667   94.0
7     102        0.8   80.0             102   8.0  0.777778  102.0
8     122        1.0  100.0             122  10.0  1.000000  122.0
9     122        1.0  100.0             122  10.0  1.000000  122.0

Now in a column PCNT_RANK you get ratio of values that are smaller or equal to the one in a column INCOME. But if you want the "interpolated" ratio, it is in column PCNT_LIN. And as you use Series.rank() for calculations, it is pretty fast and will crunch you 255M numbers in seconds.


Here I will explain how you get the value from using quantile() with linear interpolation:

temp['INCOME'].quantile(0.11)
37.93

Our data temp['INCOME'] has only ten values. According to the formula from your link to Wiki the rank of 11th percentile is

rank = 11*(10-1)/100 + 1 = 1.99

The truncated part of the rank is 1, which corresponds to the value 31, and the value with the rank 2 (i.e. next bin) is 38. The value of fraction is the fractional part of the rank. This leads to the result:

 31 + (38-31)*(0.99) = 37.93

For the values themselves, the fraction part have to be zero, so it is very easy to do the inverse calculation to get percentile:

p = (rank - 1)*100/(10 - 1)

I hope I made it more clear.

2 of 3
2

This seems to work:

A = np.sort(temp['INCOME'].values)
np.interp(sample, A, np.linspace(0, 1, len(A)))

For example:

>>> temp.INCOME.quantile(np.interp([37.5, 38, 122, 121], A, np.linspace(0, 1, len(A))))
0.103175     37.5
0.111111     38.0
1.000000    122.0
0.883333    121.0
Name: INCOME, dtype: float64

Please note that this strategy only makes sense if you want to query a large enough number of values. Otherwise the sorting is too expensive.

🌐
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)
Top answer
1 of 3
1

Pandas' describe function internally uses the quantile function. The interpolation parameter of the quantile function determines how the quantile is estimated. The output below shows how you can get 3.75 or 3.5 as the 0.75 quantile based on the interpolation used. linear is the default setting. Please take a look at Pandas' quantile function source code here 1

test = pd.Series([1,2,3,4,5,1,1,1,1,9])
test_series = test[0]

quantile_linear = test.quantile(0.75, interpolation='linear')
print(f'quantile based on linear interpolation: {quantile_linear}')

quantile based on linear interpolation: 3.75

quantile_midpoint = test.quantile(0.75, interpolation='midpoint')
print(f'quantile based on midpoint interpolation: {quantile_midpoint}')

quantile based on midpoint interpolation: 3.5

2 of 3
1

Percentiles indicate the percentage of scores that fall below a particular value. They tell you where a score stands relative to other scores.

For example: a person height 215 cm is at the 91st percentile, which indicates that his hight is higher than 91 percent of other scores.

Percentiles are a great tool to use when you need to know the position of a value/score respect to a population/data distribution you're considering. Where does a value fall within a distribution of values? While the concept behind percentiles is straight forward, there are different mathematical methods for calculating them.

In your example 50% correspond to the median of the ordered values distribution. In this case the median is calculated between two values: 1 and 2 so the median is calculated (in this case 'cause the number of values is even so the median as to be calculated between the fifth and sixth ordered values ) as the mean between them 1.5.

Find elsewhere
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.describe.html
pandas.DataFrame.describe — pandas 3.0.6 documentation
For numeric data, the result’s index will include count, mean, std, min, max as well as lower, 50 and upper percentiles. By default the lower percentile is 25 and the upper percentile is 75.
🌐
Statology
statology.org › home › how to calculate percentile rank in pandas (with examples)
How to Calculate Percentile Rank in Pandas (With Examples)
August 30, 2022 - The percentile rank of a value tells us the percentage of values in a dataset that rank equal to or below a given value.
🌐
GeeksforGeeks
geeksforgeeks.org › python › percentile-rank-of-a-column-in-a-pandas-dataframe
Percentile rank of a column in a Pandas DataFrame - GeeksforGeeks
July 15, 2025 - Let us see how to find the percentile rank of a column in a Pandas DataFrame. We will use the rank() function with the argument pct = True to find the percentile rank.
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Find the quantile with quantile() | note.nkmk.me
January 19, 2024 - import pandas as pd print(pd._... # 9 9 81 # 10 10 100 ... By default, the quantile() method on a DataFrame returns the median (the second quartile or 50th percentile) for each column....
🌐
Vultr Docs
docs.vultr.com › python › third-party › pandas › DataFrame › quantile
Python Pandas DataFrame quantile() - Compute Quantiles | Vultr Docs
December 24, 2024 - import pandas as pd data = {'scores': [23, 45, 56, 78, 89, 100, 34, 55, 77, 88]} df = pd.DataFrame(data) quantile_50 = df['scores'].quantile(0.5) print(quantile_50) Explain Code · This example calculates the 50th percentile (median) of the scores within the DataFrame.
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
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
🌐
Datacomy
datacomy.com › data_analysis › pandas › quantile
Pandas: Quantile | Datacomy
March 13, 2020 - In Pandas, the function for finding percentiles is pandas.DataFrame.quantile
🌐
Medium
medium.com › @amit25173 › understanding-pandas-dataframe-quantile-method-a4949d6807c4
Understanding pandas.DataFrame.quantile() Method | by Amit Yadav | Medium
March 6, 2025 - Let’s keep this simple: The quantile() method in pandas helps you figure out the value below which a certain percentage of your data falls. Think of it like this—if you're checking test scores, the 0.5 quantile (or the 50th percentile) is ...
🌐
Reddit
reddit.com › r/learnpython › percentile range output across multiple columns in python/pandas
r/learnpython on Reddit: Percentile range output across multiple columns in python/pandas
February 6, 2021 -

I have a dataset, df, where I would like to showcase the 60th, 70th, and 90th percentile values for given values in a column

DATA

type value

Hello 1

Hello 2

Hello 3

Hello 5

Hello 5

Hello 6

Hello 8

Hello 8

Hello 3

OK 1

OK 1

OK 2

OK 2

DESIRED

type 0.6 0.7 0.9

Hello 5 5.6 8

OK 1.8 2 2

DOING

My approach is to utilize the percentile function in numpy:

import numpy as np

print np.percentile(df,60)

print np.percentile(df,70)

print np.percentile(df,90)

This works, however, the output shows these values individually and does not maintain the other columns in the dataset

🌐
GitHub
gist.github.com › sbatururimi › c5dd304ce70fc71bd95ae1ada0acc614
Compute percentiles per group in pandas dataframe · GitHub
Compute percentiles per group in pandas dataframe. GitHub Gist: instantly share code, notes, and snippets.
🌐
Pythontic
pythontic.com › pandas › dataframe-computations › quantile
Computing quantile values for a pandas DataFrame | Pythontic.com
The quantile() function of Pandas DataFrame class computes the value below which a given portion of the data lies. The example 2 plots a normal distribution for a value of student scores and marks the deciles in the normal curve as given in the diagram below.
🌐
datagy
datagy.io › home › pandas tutorials › data analysis in pandas › pandas quantile: calculate percentiles of a dataframe
Pandas Quantile: Calculate Percentiles of a Dataframe • datagy
April 16, 2023 - Beginning in April 2023 with Pandas 2.0, the default argument for numeric_only is set to False. This has a big impact on legacy code, forcing your code to be more explicit. q=[0.5]: a float or an array that provides the value(s) of quantiles to calculate · axis=[0]: the axis to calculate the percentiles on (0 for row-wise and 1 for column-wise)
🌐
Univ-dschang
siges-copy.univ-dschang.org › blog › mastering-pandas-quantile-a-complete-guide-1767646977
Mastering Pandas Quantile: A Complete Guide
January 6, 2026 - Univ-dschang brings you the latest in news, sports, and entertainment, all from a unique. Stay informed, stay entertained.