.corr() pandas – Python Pandas Series corr() Function

Pandas Series corr() Function:

.corr() pandas: The corr() function of the Pandas Series calculates the correlation of a  Series with other Series while ignoring missing values. The computation automatically excludes NA and null values.

Syntax:

Series.corr(other, method='pearson', min_periods=None)

Parameters

other: This is required. It indicates a Series with which the correlation has to be computed.

method: This is optional. It is the correlation method. The default value is ‘pearson.’ Possible values include:

  • Pearson – Coefficient of standard correlation
  • kendall  – Coefficient of Kendall Tau correlation
  • Spearman- Spearman rank correlation
  • callable – callable with two 1d ndarrays as input and returns a float. Note that regardless of how the callable behaves, the returned correlation matrix will have 1 along the diagonals and will be symmetric.

min_periods: This is optional. It is an int indicating the minimum number of observations required for a valid result.

Return Value:

The correlation of a Series with other Series is returned by the corr() function of the Pandas Series.

Python Pandas Series corr() Function

Example1

Approach:

  • Import pandas module using the import keyword.
  • Pass some random list as an argument to the Series() function of the pandas module to create a series.
  • Store it in a variable.
    Pass some random list as an argument to the Series() function of the pandas module to create another series.
  • Store it in another variable.
  • Print the above given first series
  • Print the above given second series
  • Apply corr() function on the given first and second series to get the correlation of the first series with the second and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random list as an argument to the Series() function
# of the pandas module to create a series.
# Store it in a variable.
gvn_series1 = pd.Series([3, 3.2, 10.1, 5.3, 4, 2, 3, 6])
# Pass some random list as an argument to the Series() function
# of the pandas module to create another series.
# Store it in another variable.
gvn_series2 = pd.Series([3.1, 3, 7.2, 6, 8, 2.5, 3, 1.1])
# Print the above given first series
print("The given first series is:")
print(gvn_series1)
print()
# Print the above given second series
print("The given second series is:")
print(gvn_series2)
print()
# Apply corr() function on the given first and second series to get the 
# correlation of first series with the second
# and print the result.
print("The correlation of first series with the second:")
print(gvn_series1.corr(gvn_series2))

Output:

he given first series is:
0     3.0
1     3.2
2    10.1
3     5.3
4     4.0
5     2.0
6     3.0
7     6.0
dtype: float64

The given second series is:
0    3.1
1    3.0
2    7.2
3    6.0
4    8.0
5    2.5
6    3.0
7    1.1
dtype: float64

The correlation of first series with the second:
0.46464053797455707

Example2

Corr python: Here, the corr() function is used on a specific series/column in a DataFrame.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe.
  • Apply corr() function on the student_rollno and student_marks column of the dataframe to get the correlation of student_rollno column of the dataframe with the student_marks and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "student_rollno": [1, 2, 3, 4],
  "student_marks": [80, 35, 25, 90]},
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()
# Apply corr() function on the student_rollno and student_marks column of the 
# dataframe to get the correlation of student_rollno column of the dataframe 
# with the student_marks and print the result.
print("The correlation of student_rollno column of the dataframe with the student_marks:")
print(data_frme['student_rollno'].corr(data_frme['student_marks']))

Output:

The given Dataframe:
        student_rollno  student_marks
virat                1             80
nick                 2             35
jessy                3             25
sindhu               4             90

The correlation of student_rollno column of the dataframe with the student_marks:
0.08