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.

## How to calculate 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 –

**📚 Discover Online Data Science Courses & Programs (Enroll for Free)**

**Introductory ⭐**

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# 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)

## Examples

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

### 1. 95th percentile of an array or list using numpy

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:

**Upskill your career right now →**

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.

### 2. Different percentile values of the same array

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.

**Upskill your career right now →**

### 3. Nth quantile of a pandas series

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:

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

## How are percentiles useful?

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.**