# How to Calculate Percentiles in Python

Percentiles are statistical measures used to interpret and understand datasets. They can provide insights into the distribution of data, specifically where a particular value falls within the dataset. For instance, if your score is in the 90th percentile on a test, it means you scored better than 90% of the other test-takers. In this article, we will discuss how to calculate percentiles in Python using various methods and libraries.

### Basics of Percentiles

Before diving into the Python code, let’s understand some basics of percentiles. The nth percentile of a set of data is the value at which n percent of the data is below it. So, the 25th percentile is the point at which 25% of the data is below and 75% is above it. This percentile is also known as the first quartile, or Q1. Similarly, the 50th percentile (also known as the median or Q2), is the point at which 50% of the data is below and 50% is above it, and the 75th percentile (Q3) is the point at which 75% of the data is below and 25% is above it.

### Using NumPy to Calculate Percentiles

NumPy is a popular Python library used for numerical computations. Its function numpy.percentile() is often used to calculate percentiles.

Here’s how to calculate percentiles with numpy.percentile():

import numpy as np

# Assume we have the following data:
data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

# To find the 50th percentile (the median), we use:
p50 = np.percentile(data, 50)
print('50th percentile: ', p50)

# Similarly, to find the 25th and 75th percentiles, we use:
p25 = np.percentile(data, 25)
p75 = np.percentile(data, 75)
print('25th percentile: ', p25)
print('75th percentile: ', p75)

This program will output:

50th percentile: 5.5
25th percentile: 3.25
75th percentile: 7.75

### Using Pandas to Calculate Percentiles

Pandas is another Python library that’s widely used for data manipulation and analysis. It’s especially useful for handling large datasets. You can use the quantile() function in Pandas to calculate percentiles.

Here’s how:

import pandas as pd

# Assume we have the following data:
data = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

# To find the 50th percentile (the median), we use:
p50 = data.quantile(0.50)
print('50th percentile: ', p50)

# Similarly, to find the 25th and 75th percentiles, we use:
p25 = data.quantile(0.25)
p75 = data.quantile(0.75)
print('25th percentile: ', p25)
print('75th percentile: ', p75)

This program will output the same as the previous one.

50th percentile: 5.5
25th percentile: 3.25
75th percentile: 7.75

### Using SciPy to Calculate Percentiles

SciPy is a scientific computation library that builds on NumPy. It’s used for more specialized tasks that aren’t covered by NumPy. Its function scipy.stats.percentileofscore() can be used to compute percentiles.

Here’s how:

from scipy import stats

# Assume we have the following data:
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# To find the percentile rank of value 6 in the data, we use:
p = stats.percentileofscore(data, 6)
print('Percentile rank: ', p)

This program will output:

Percentile rank:  50.0

This implies that the value 6 is at the 50th percentile of the data.

### Handling 2D data

If you’re working with a 2D array (or matrix) and want to find percentiles along a specific axis, NumPy’s percentile() function can do that as well.

import numpy as np

# Assume we have the following 2D data:
data = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])

# To find the 50th percentile for each column, we use:
p50_column = np.percentile(data, 50, axis=0)
print('50th percentile (column): ', p50_column)

# Similarly, to find the 50th percentile for each row, we use:
p50_row = np.percentile(data, 50, axis=1)
print('50th percentile (row): ', p50_row)

This program will output:

50th percentile (column): [3.5 4.5 5.5 6.5 7.5]
50th percentile (row): [3. 8.]

### Conclusion

Percentiles are a fundamental concept in statistics, helping to understand and interpret datasets. Python, with its powerful libraries such as NumPy, Pandas, and SciPy, makes the computation of percentiles straightforward and efficient.

It’s important to remember that percentiles, like any statistical measure, don’t tell the full story of your data. However, they can provide you with valuable insights that help you dig deeper into your data analysis.

Whether you’re preparing a data report, analyzing test scores, or building a machine learning model, knowing how to calculate percentiles in Python is a handy skill to have in your data analysis toolkit.