How does the series.corr() method work in pandas?



The pandas.Series.corr() method used to compute the correlation between two series objects with excluding missing values. As a result, It returns a float value that varies from -1 to 1. If the output is an integer 1, which indicates the relation between two series is a strong positive relationship and if it is “-1”, which means the relationship is a strong negative relation.

The series.corr() method has three parameters first one is another series object, the second one is the name of the correlation method, and the third one is min_period which is an optional one.

Example 1

import pandas as pd # create pandas Series1 series1 = pd.Series([9,2,4,6,1]) print("First series object:",series1) # create pandas Series2 series2 = pd.Series([12,4,2,7,4]) print("Second series object:",series2) # calculate the correlation print("The Correlation value: ", series1.corr(series2, method='pearson'))

Explanation

Initially, we have created two pandas Series objects using a python list of integers. After that, we find out the correlation between values of both series objects using the “Pearson” method.

Output

First series object: 0 9 1 2 2 4 3 6 4 1 dtype: int64 Second series object: 0 12 1 4 2 2 3 7 4 4 dtype: int64 The Correlation value: 0.8471600336634684

The correlation between two series objects for the following example is “0.85”, which indicates the two series objects are having strong positive relation.

Example 2

import pandas as pd import numpy as np # create pandas Series1 series1 = pd.Series([12,np.nan,47,19,10]) print("First series object:",series1) # create pandas Series2 series2 = pd.Series([9,4,2,np.nan,4]) print("Second series object:",series2) # calculate the correlation print("The Correlation value: ", series1.corr(series2, method='pearson'))

Explanation

Initially, we have created two pandas Series objects using a python list of integers and it is also having some null values created by numpy.nan attribute. After that, we find out the correlation between values of both series objects using the “Pearson” method again.

Output

First series object: 0 12.0 1  NaN 2 47.0 3 19.0 4 10.0 dtype: float64 Second series object: 0 9.0 1 4.0 2 2.0 3 NaN 4 4.0 dtype: float64 The Correlation value: -0.6864226486537492

The correlation between the two series objects of the following example is “-0.69”, which indicates the two series objects are having strong negative relation.

Updated on: 2022-03-09T07:44:51+05:30

415 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements