Pandas Tutorial Pandas References

Pandas Series - cov() function



The Pandas Series cov() function computes covariance of a Series with other Series, excluding missing values. Both NA and null values are automatically excluded from the calculation.

Syntax

Series.cov(other, min_periods=None, ddof=1)

Parameters

other Required. Specify a Series with which to compute the covariance.
min_periods Optional. An int to specify minimum number of observations required to have a valid result.
ddof Optional. Specify Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements.

Return Value

Returns covariance with other.

Example: using cov() on a Series

In the example below, the cov() function is used to calculate the covariance of given series.

import pandas as pd
import numpy as np

GDP = pd.Series([1.02, 1.03, 1.04, 0.98])
HDI = pd.Series([1.02, 1.01, 1.02, 1.03])

print("The GDP contains:")
print(GDP, "\n")

print("The HDI contains:")
print(HDI, "\n")

#calculating covariance 
print("GDP.cov(HDI) returns:")
print(GDP.cov(HDI))

The output of the above code will be:

The GDP contains:
0    1.02
1    1.03
2    1.04
3    0.98
dtype: float64 

The HDI contains:
0    1.02
1    1.01
2    1.02
3    1.03
dtype: float64 

GDP.cov(HDI) returns:
-0.00016666666666666696

Example: using cov() on selected series in a DataFrame

Similarly, the cov() function can be applied on selected series/column of a given DataFrame. Consider the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "GDP": [1.02, 1.03, 1.04, 0.98],
  "GNP": [1.05, 0.99, np.nan, 1.04],
  "HDI": [1.02, 1.01, 1.02, 1.03],
  "Agriculture": [1.02, 1.02, 0.99, 0.98]},
  index= ["Q1", "Q2", "Q3", "Q4"]
)

print("The DataFrame is:")
print(df)

#covariance matrix using GDP and HDI series
print("\ndf['GDP'].cov(df['HDI']) returns:")
print(df['GDP'].cov(df['HDI']))

The output of the above code will be:

The DataFrame is:
     GDP   GNP   HDI  Agriculture
Q1  1.02  1.05  1.02         1.02
Q2  1.03  0.99  1.01         1.02
Q3  1.04   NaN  1.02         0.99
Q4  0.98  1.04  1.03         0.98

df['GDP'].cov(df['HDI']) returns:
-0.00016666666666666696

❮ Pandas Series - Functions

5