- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- How does the series.copy() method work in Pandas?
- How does the series.cummax() method work in Pandas?
- How does the series.cumprod() method work in Pandas?
- How does the series.cumsum() method work in Pandas?
- How does the pandas series.ffill() method work?
- How does the pandas series.expanding() method work?
- How does the pandas series.first_valid_index() method work?
- How does the series.cummim() method work in Pandas?\n
- How does the pandas Series idxmax() method work?
- How does the pandas Series idxmin() method work?
- How does the pandas series.divmod() method work?\n
- How does pandas series astype() method work?
- How does pandas series combine() method work?
- How does pandas series combine_first() method work?
- How does pandas series div() method work?
