To find the percentile of a value relative to an array (or in your case a dataframe column), use the scipy function stats.percentileofscore().

For example, if we have a value x (the other numerical value not in the dataframe), and a reference array, arr (the column from the dataframe), we can find the percentile of x by:

from scipy import stats
percentile = stats.percentileofscore(arr, x)

Note that there is a third parameter to the stats.percentileofscore() function that has a significant impact on the resulting value of the percentile, viz. kind. You can choose from rank, weak, strict, and mean. See the docs for more information.

For an example of the difference:

>>> df
   a
0  1
1  2
2  3
3  4
4  5

>>> stats.percentileofscore(df['a'], 4, kind='rank')
80.0

>>> stats.percentileofscore(df['a'], 4, kind='weak')
80.0

>>> stats.percentileofscore(df['a'], 4, kind='strict')
60.0

>>> stats.percentileofscore(df['a'], 4, kind='mean')
70.0

As a final note, if you have a value that is greater than 80% of the other values in the column, it would be in the 80th percentile (see the example above for how the kind method affects this final score somewhat) not the 20th percentile. See this Wikipedia article for more information.

Answer from wingr on Stack Overflow
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.Series.quantile.html
pandas.Series.quantile โ€” pandas 3.0.6 documentation
linear: i + (j - i) * (x-i)/(j-i), ... part of the index surrounded by i > j. ... 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 ...
๐ŸŒ
Medium
medium.com โ€บ @amit25173 โ€บ understanding-percentiles-in-pandas-369166d19e76
Understanding Percentiles in Pandas | by Amit Yadav | Medium
March 6, 2025 - For example, the 75th percentile (also known as the third quartile) means 75% of the data lies below that value. Quick Example: You might be wondering how this looks in practice. Hereโ€™s a simple dataset of test scores: import numpy as np # Sample scores scores = [55, 65, 75, 85, 95] # Finding the 90th percentile percentile_90 = np.percentile(scores, 90) print("90th Percentile:", percentile_90)
Discussions

python - Find percentile stats of a given column - Stack Overflow
You can even give multiple columns with null values and get multiple quantile values (I use 95 percentile for outlier treatment) More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How do I get the percentile for a row in a pandas dataframe? - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. CopyExample DataFrame Values - 0 78 1 38 2 42 3 48 4 31 5 89 6 94 7 102 8 122 9 122 stats.percentileofscore... More on stackoverflow.com
๐ŸŒ stackoverflow.com
how to calculate IV percentile in pandas dataframe
import pandas as pd import numpy as np # Random IV distribution, probably not normal df = pd.DataFrame({ 'date': pd.date_range(start='2022-01-01', end='2022-03-01', freq='B'), 'iv': np.random.normal(0.2, 0.1, 42) }) df.set_index('date', inplace=True) # Set the number of days you want to calculate over window = 10 # Calculate the IV rank for each day df['iv_rank'] = df['iv'].rolling(window).apply(lambda x: pd.Series(x).rank(pct=True).iloc[-1]) # Calculate the numerator for each day df['days_under_iv'] = df['iv'].rolling(window).apply(lambda x: sum(x < x[-1])) df['trading_days_window'] = window # Calculate the IV percentile for each day df['iv_percentile'] = (df['days_under_iv'] / df['trading_days_window']) * 100 # Drop the NaN values resulting from the rolling window df.dropna(inplace=True) print(df.head()) Is this what you are asking for? I assumed a rolling window, but you might have wanted a static calculation. More on reddit.com
๐ŸŒ r/quant
2
6
March 23, 2023
How to find a value for 90% of my data.
This is just percentiles. So you want to find the value that's at the 90th (the point at which 90% of the sample had a TAT at that value or lower) or 10th (point at which 90% of the sample had a TAT at that value or higher) percentile, depending on which way you're trying to get at. You can use the quantiles function, so: quantile(data$TAT, probs = (.1)) or quantile(data$TAT, probs = (.9)) edit: upon re-reading your question, I'm pretty sure what you're after is the 90th percentile. To say "90% of our sample completed task x within y minutes/seconds/whatever." More on reddit.com
๐ŸŒ r/datasets
4
2
February 2, 2022
Top answer
1 of 5
45

To find the percentile of a value relative to an array (or in your case a dataframe column), use the scipy function stats.percentileofscore().

For example, if we have a value x (the other numerical value not in the dataframe), and a reference array, arr (the column from the dataframe), we can find the percentile of x by:

from scipy import stats
percentile = stats.percentileofscore(arr, x)

Note that there is a third parameter to the stats.percentileofscore() function that has a significant impact on the resulting value of the percentile, viz. kind. You can choose from rank, weak, strict, and mean. See the docs for more information.

For an example of the difference:

>>> df
   a
0  1
1  2
2  3
3  4
4  5

>>> stats.percentileofscore(df['a'], 4, kind='rank')
80.0

>>> stats.percentileofscore(df['a'], 4, kind='weak')
80.0

>>> stats.percentileofscore(df['a'], 4, kind='strict')
60.0

>>> stats.percentileofscore(df['a'], 4, kind='mean')
70.0

As a final note, if you have a value that is greater than 80% of the other values in the column, it would be in the 80th percentile (see the example above for how the kind method affects this final score somewhat) not the 20th percentile. See this Wikipedia article for more information.

2 of 5
5

Probably very late but still

df['column_name'].describe()

will give you the regular 25, 50 and 75 percentile with some additional data but if you want percentiles for some specific values then

df['column_name'].describe(percentiles=[0.1, 0.2, 0.3, 0.5])

This will give you 10th, 20th, 30th and 50th percentiles. You can give as many values as you want.

The resulting object can be accessed like a dict:

desc = df['column_name'].describe(percentiles=[0.1, 0.2, 0.3, 0.5])
print(desc)
print(desc['10%'])
๐ŸŒ
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 - This is where the interpolation= parameter comes into play. By default, Pandas will use a linear interpolation to generate the percentile, meaning it will treat the values as linear and find the linearly interpolated value.
๐ŸŒ
Statology
statology.org โ€บ home โ€บ how to calculate percentiles in python (with examples)
How to Calculate Percentiles in Python (With Examples)
November 3, 2020 - The following code shows how to find the 95th percentile value for a several columns in a pandas DataFrame: import numpy as np import pandas as pd #create DataFrame df = pd.DataFrame({'var1': [25, 12, 15, 14, 19, 23, 25, 29, 33, 35], 'var2': [5, 7, 7, 9, 12, 9, 9, 4, 14, 15], 'var3': [11, 8, 10, 6, 6, 5, 9, 12, 13, 16]}) #find 95th percentile of each column df.quantile(.95) var1 34.10 var2 14.55 var3 14.65 #find 95th percentile of just columns var1 and var2 df[['var1', 'var2']].quantile(.95) var1 34.10 var2 14.55 ยท
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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ percentile-rank-of-a-column-in-a-pandas-dataframe
Percentile rank of a column in a Pandas DataFrame - GeeksforGeeks
August 17, 2020 - 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.
Find elsewhere
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ find-percentile-stats-of-a-given-column.aspx
Pandas: Find percentile stats of a given column
September 24, 2023 - This method returns the median of the values over the requested axis. DataFrame.median( axis=NoDefault.no_default, skipna=True, level=None, numeric_only=None, **kwargs ) This method returns the mode(s) of each element along the selected axis. DataFrame.mode( axis=0, numeric_only=False, dropna=True ) ... # Importing pandas package import pandas as pd # Creating a dictionary d = { 'A':[90,72,56,76,82,34], 'B':[50,56,72,80,53,78] } # Creating a DataFrame df = pd.DataFrame(d,index=['a','b','c','d','e','f']) # Display original DataFrame print("Original DataFrame:\n",df,"\n") # Get multiple statistical result print("MEAN:\n",df['A'].mean(),"\n") print("MEAN:\n",df['A'].median(),"\n") # Calculating percentile using quantile print("10th Percentile:\n",df['A'].quantile(0.1),"\n") print("50th Percentile:\n",df['A'].quantile(0.5),"\n") print("90th Percentile:\n",df['A'].quantile(0.9),"\n")
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ how-to-find-percentile-stats-of-a-given-column-using-pandas
How to Find Percentile Stats of a Given Column Using Pandas | Saturn Cloud Blog
May 1, 2026 - The quantile() function is used to find the percentile statistics of a given column in a Pandas DataFrame. We can use this function to find any percentile, such as the median (50th percentile), first quartile (25th percentile), third quartile ...
๐ŸŒ
Data Science Parichay
datascienceparichay.com โ€บ home โ€บ blog โ€บ calculate percentile in python
Calculate Percentile in Python - Data Science Parichay
October 9, 2021 - There are a number of ways. You can use the numpy percentile() function on array or sequence of values. You can also use the pandas quantile() function to get the nth percentile of a pandas series.
๐ŸŒ
Codegive
codegive.com โ€บ blog โ€บ pandas_find_percentile_of_value.php
Pandas Find Percentile of Value (2024): Unlock Data Insights & Master Percentile Ranking
To find the percentile rank of a specific value in a pandas Series or DataFrame column, first calculate the percentage ranks of all values using df['column'].rank(pct=True), then locate the rank corresponding to your target value.
๐ŸŒ
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. You can use the following methods to calculate percentile rank in pandas:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ calculate-arbitrary-percentile-on-pandas-groupby
Calculate Arbitrary Percentile on Pandas GroupBy - GeeksforGeeks
July 23, 2025 - To calculate an arbitrary percentile for each group, you can use the quantile() function in combination with groupby(). The quantile() function takes a value between 0 and 1, where 0.5 represents the median (50th percentile), 0.9 represents ...
๐ŸŒ
DataScience Made Simple
datasciencemadesimple.com โ€บ home โ€บ percentile rank of a column in pandas python โ€“ (percentile value)
Percentile rank of a column in pandas python - (percentile value) - DataScience Made Simple
November 15, 2019 - import pandas as pd import numpy ... Percentile rank of the column (Mathematics_score) is computed using rank() function and with argument (pct=True), and stored in a new column namely โ€œpercentile_rankโ€ as shown ...
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.

๐ŸŒ
Statology
statology.org โ€บ home โ€บ pandas: how to use describe() with specific percentiles
Pandas: How to Use describe() with Specific Percentiles
March 8, 2023 - Note: The describe() function also returns the 50th percentile because this represents the median value for each variable and it is one of the default metrics calculated by the describe() function.