Calculate Percentile in Python

Percentiles are descriptive statistics that tell us about the distribution of the values. The nth percentile value denotes that n% of the values in the given sequence are smaller than this value. For example, the 25th percentile value is the value that is greater than 25% of the values present in the data. In this tutorial, we will look at how to calculate the nth percentile value (for example, the 95th percentile) in Python.

calculate the 50th percentile in python

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. The following is the syntax for both –

# using numpy - 95th percentile value of the array arr
np.percentile(arr, 95)
# using pandas - 95th percentile value of column 'Col' in df
df['Col'].quantile(0.95)

Let’s look at some examples of using the above syntax to get the percentiles in Python.

To get the nth percentile value of an array or a list, pass the array (or list) along with the value of n (the percentile you want to calculate) to the numpy’s percentile() function. For example, let’s get the 95th percentile value of an array of the first 100 natural numbers (numbers from 1 to 100).

import numpy as np

# create a numpy array
arr = np.array(range(1, 101))
# get the 95th percentile value
print(np.percentile(arr, 95))

Output:

95.05

You can see that we get 95.05 as the output. Notice that 95% of the values in the array of first 100 natural numbers are smaller than this value.

The above function would work similarly on a list.

import numpy as np

# create a list of 100 numbers
ls = list(range(1, 101))
# get the 95th percentile value
print(np.percentile(ls, 95))

Output:

95.05

We get the same result as above.

You can get the value for different percentiles by passing a list of the percentiles you want. For example, let’s get the 25th, 50th and the 75th percentile values for the same array (first 100 natural numbers).

import numpy as np

# create a numpy array
arr = np.array(range(1, 101))
# get the 25th, 50th, and 75th percentile values
print(np.percentile(arr, [25, 50, 75]))

Output:

[25.75 50.5  75.25]

We get the values representing the 25th, 50th, and the 75th percentile of the array respectively.

You can also use the pandas quantile() function to get the nth percentile of a pandas series or a dataframe in python. First, let’s create a sample dataframe.

import pandas as pd

# create a pandas dataframe
df = pd.DataFrame({
    'Day' : [i for i in range(1, 101)],
    'Next Day': [i+1 for i in range(1, 101)],
    'Location': ['Japan'] * 100
})
# display the dataframe
df

Output:

Pandas dataframe with two numerical and one text column.

Here, we created a pandas dataframe of two numerical columns and one text column. Let’s now calculate the 95th percentile value for the “Day” column. Note that when using the pandas quantile() function pass the value of the nth percentile as a fractional value. For example, pass 0.95 to get the 95th percentile value.

# get the 95th percentile value of "Day"
df['Day'].quantile(0.95)

Output:

95.05

You can also apply the same function on a pandas dataframe to get the nth percentile value for every numerical column in the dataframe.

# get the 95th percentile value of each numerical column
df.quantile(0.95)

Output:

Day         95.05
Next Day    96.05
Name: 0.95, dtype: float64

Here you can see that we got the 95th percentile values for all the numerical columns in the dataframe.

You can also get multiple quantiles at a time. For example, let’s get the 25th, 50th and the 75th percentile value of the “Day” column.

# get different quantiles for "Day"
df['Day'].quantile([0.25, 0.5, 0.75])

Output:

0.25    25.75
0.50    50.50
0.75    75.25
Name: Day, dtype: float64

Some percentile values can give you important descriptive information about the distribution of the underlying data. For example, the median can be a good measure of central tendency (can be very useful if your data has outliers that can skew the mean), the difference of the 75th and the 25th percentile value gives you the Inter Quartile Range which is a measure of the spread in the data (how spread out your data is).

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5 and pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Leave a Reply

Your email address will not be published. Required fields are marked *