How to find the covariance using the Series.cov() Method in Pandas?


The Series.cov() method in pandas is used to calculate the Covariance of the Series with the other Series by excluding null/NA or missing values.

The covariance is a way of calculating the relationship between two random variables and it will tell us how much two random variables vary together.

The output for this cov() method is a floating-point value that represents the covariance between two Series.

This method has three parameters, which are other, min_period, and ddof.

Example 1

import pandas as pd
import numpy as np
# create pandas Series1
series1 = pd.Series([12,34,65,21])

print("First series object:",series1)

# create pandas Series2
series2 = pd.Series([9,78,62,12])

print("Second series object:",series2)

# calculate the covariance value
print("The covariance value: ", series1.cov(series2))

Explanation

We have initialized two pandas series object series1 and series2 by using a list of integers, then calculated the covariance by applying the cov() method.

Output

First series object:
0 12
1 34
2 65
3 21
dtype: int64

Second series object:
0  9
1 78
2 62
3 12
dtype: int64

The covariance value: 576.3333333333333

The covariance for the above example is 576.33 which is displayed in the above output block.

Example 2

import pandas as pd
import numpy as np

# create pandas Series1
series1 = pd.Series([89,np.nan,74,91,100])

print("First series object:",series1)

# create pandas Series2
series2 = pd.Series([93,54,21,80,42])

print("Second series object:",series2)

# calculate the covariance value
print("The covariance value: ", series1.cov(series2, min_periods=3))

Explanation

In the following example we are calculating the covariance between two series objects, here there is a Nan value present in the elements of a given series object.

Output

First series object:
0 89.0
1  NaN
2 74.0
3 91.0
4 100.0
dtype: float64

Second series object: 0 93
1 54
2 21
3 80
4 42
dtype: int64

The covariance value: 141.66666666666666

The series.cov() method excludes the Nan values while calculating the covariance between series. For the above example, the covariance is “141.666”.

Updated on: 09-Mar-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements