Pandas: How to compute correlation between 2 Series

Updated: February 18, 2024 By: Guest Contributor Post a comment

Overview

Understanding the relationship between two datasets or variables is a common task in data analysis, providing insights into how one variable moves in relation to another. One of the fundamental statistical measures for this purpose is correlation, which quantifies the degree to which two variables move in relation to one another. In Python, the Pandas library simplifies data manipulation and analysis, offering powerful methods to compute correlation between two Series.

Prerequisites

To follow this tutorial, it’s assumed you have a basic understanding of Python and data analysis. You should have Python installed on your computer, along with the Pandas library. If you haven’t already installed Pandas, you can do so by running pip install pandas in your terminal or command prompt.

Creating the Series

Before we dive into computing correlation, let’s first generate two Pandas Series. A Series is a one-dimensional labeled array capable of holding any data type:

import pandas as pd
import numpy as np

# Creating two Series with random numbers
data1 = np.random.rand(100)
data2 = np.random.rand(100)

s1 = pd.Series(data1)
s2 = pd.Series(data2)

We now have two Series, s1 and s2, each containing 100 random numbers.

Basic Correlation Computation

The most common method to compute correlation is Pearson’s correlation coefficient, which measures the linear correlation between two datasets. The value ranges from -1 to 1, where 1 means total positive linear correlation, 0 no linear correlation, and -1 total negative linear correlation.

# Computing Pearson correlation coefficient

pearson_corr = s1.corr(s2)
print(f"Pearson correlation coefficient: {pearson_corr}")

This single line of code computes the Pearson correlation coefficient between our two Series. Depending on the random values, you will get a number indicating the degree of correlation.

Spearman’s Rank Correlation

Another type of correlation is Spearman’s rank correlation, which assesses how well the relationship between two variables can be described using a monotonic function. It’s particularly useful when your data is not normally distributed or when the relationship is nonlinear. To compute it in Pandas:

# Computing Spearman's rank correlation

spearman_corr = s1.corr(s2, method='spearman')
print(f"Spearman's rank correlation: {spearman_corr}")

Again, we can directly use the .corr() method, specifying method='spearman' to compute Spearman’s rank correlation.

Visualizing Correlation

Visualizing data can provide additional insights beyond the numerical correlation coefficients. Let’s visualize the relationship between our two Series using the Matplotlib library:

import matplotlib.pyplot as plt

# Scatter plot
plt.scatter(s1, s2)
plt.title('Scatter plot of two Series')
plt.xlabel('s1')
plt.ylabel('s2')
plt.show()

Advanced Correlation Analysis

For a more comprehensive analysis, you might want to consider computing correlation matrices, especially when dealing with multiple series or dataset columns. You can use the DataFrame.corr() method to compute pairwise correlation of columns, excluding NA/null values.

Let’s create a DataFrame from our two Series and compute the correlation matrix:

df = pd.DataFrame({'s1': s1, 's2': s2})
correlation_matrix = df.corr()
print(correlation_matrix)

This provides us with a matrix showing the Pearson correlation coefficients between all pairs of columns within the DataFrame.

Handling Missing Values

In real-world data, missing values are common. It’s important to handle these effectively when computing correlation coefficients. Pandas automatically excludes null or missing values when computing correlation. However, you should ensure that your data cleaning process effectively manages missing values before performing any correlation analysis to avoid biased results.

Conclusion

Computing correlation between two Pandas Series is a straightforward process that provides valuable insights into the linear or monotonic relationship between datasets. Whether using Pearson’s or Spearman’s measures, Pandas offers a seamless way to quantify and visualize these relationships, making it an invaluable tool in any data analyst’s repertoire.