How to Calculate Covriance in dataframes | pandas.DataFrame.cov() function

Spread the love

pandas.DataFrame.cov(): This function compute the pairwise covariance among the series of a DataFrame. The returned data frame is the covariance matrix of the columns of the DataFrame.
Syntax: DataFrame.cov(min_periods=None):
Compute pairwise covariance of columns, excluding NA/null values.

Both NA and null values are automatically excluded from the calculation. (See the note below about bias from missing values.) A threshold can be set for the minimum number of observations for each value created. Comparisons with observations below this threshold will be returned as NaN.

This method is generally used for the analysis of time series data to understand the relationship between different measures across time.

min_periods : int, optional
Minimum number of observations required per pair of columns to have a valid result.

The covariance matrix of the series of the DataFrame.

pandas.DataFrame.cov() function example program

import numpy as np import pandas as pd df = pd.DataFrame({'A': [5, 2,56,67], 'B': [4, 8,88,99]}) print(df.cov())


             A            B
A  1143.000000  1709.833333
B  1709.833333  2574.916667




Leave a Reply

Your email address will not be published. Required fields are marked *