# How to Calculate the Interquartile Range in Python

### Introduction

The interquartile range (IQR) is a fundamental concept in statistics that describes the spread of a dataset. It represents the range where the middle 50% of the values fall. This range is considered to be the most reliable because it’s not influenced by outliers or extreme values in the same way as other measures like the range or variance. In other words, the IQR provides a more stable and reliable measure of the dispersion or spread of a dataset.

In this article, we will explore different ways to calculate the Interquartile Range (IQR) in Python, using several libraries, namely numpy, scipy, and pandas. We will also understand how to plot a boxplot using the matplotlib and seaborn libraries, which is an effective way to visualize the IQR.

### Calculating the Interquartile Range (IQR)

The interquartile range is calculated by finding the difference between the 75th percentile (third quartile) and the 25th percentile (first quartile).

The formula for calculating IQR is:

IQR = Q3 - Q1

Where:

• Q1 = first quartile (25th percentile)
• Q3 = third quartile (75th percentile)

The following code block shows the basic method to calculate IQR in Python without using any specific statistical libraries:

data = [1, 2, 5, 6, 7, 9, 12, 15, 18, 19, 27]

# Calculate Q1 and Q3
Q1 = sorted(data)[int(len(data) * 0.25)]
Q3 = sorted(data)[int(len(data) * 0.75)]

# Calculate the IQR
IQR = Q3 - Q1
print("Interquartile Range:", IQR)

This code sorts the dataset, then multiplies the length of the dataset by 0.25 and 0.75 to get the first and third quartiles. However, this method is not entirely accurate, especially for datasets that do not divide evenly. Therefore, it’s better to use statistical libraries to calculate the IQR, as we’ll see next.

### Using NumPy

NumPy is a powerful library in Python for scientific computing. It supports operations such as sorting, computations on arrays, and various mathematical operations.

Here’s how you can compute IQR using NumPy:

import numpy as np

data = [1, 2, 5, 6, 7, 9, 12, 15, 18, 19, 27]

# Calculate Q1 and Q3
Q1 = np.percentile(data, 25)
Q3 = np.percentile(data, 75)

# Calculate the IQR
IQR = Q3 - Q1
print("Interquartile Range:", IQR)

### Using SciPy

SciPy is another scientific computation library that has a built-in method for calculating the IQR, which can save a few lines of code. Here’s how you can do it:

from scipy import stats

data = [1, 2, 5, 6, 7, 9, 12, 15, 18, 19, 27]

# Calculate the IQR
IQR = stats.iqr(data)
print("Interquartile Range:", IQR)

### Using Pandas

Pandas is a popular library in Python for data manipulation and analysis. Here’s how you can compute the IQR using pandas:

import pandas as pd

data = [1, 2, 5, 6, 7, 9, 12, 15, 18, 19, 27]

# Create a pandas series from the data
data_series = pd.Series(data)

# Calculate Q1 and Q3
Q1 = data_series.quantile(0.25)
Q3 = data_series.quantile(0.75)

# Calculate the IQR
IQR = Q3 - Q1
print("Interquartile Range:", IQR)

### Visualizing IQR Using Boxplots

The IQR is one of the key elements in a boxplot, which is a standardized way of displaying the dataset based on a five-number summary: minimum, first quartile, median, third quartile, and maximum.

Let’s create a boxplot for our data using matplotlib and seaborn.

import matplotlib.pyplot as plt
import seaborn as sns

data = [1, 2, 5, 6, 7, 9, 12, 15, 18, 19, 27]

plt.figure(figsize=(10, 5))
sns.boxplot(data, color='lightblue')
plt.title("Boxplot of Data")
plt.show()

In the boxplot, the box represents the IQR, the line inside the box is the median, the whiskers represent the range of the data, and any dots outside the whiskers represent outliers.

### Conclusion

In this tutorial, we have learned how to calculate the Interquartile Range (IQR) in Python using various libraries such as numpy, scipy, and pandas. We also looked at how to visualize the IQR using a boxplot with matplotlib and seaborn. Calculating the IQR is a fundamental step in understanding the spread and skewness of your data, and Python provides several ways to do this efficiently.