Python Pandas Series cov() Function

Pandas Series cov() Function:

The cov() function of the Pandas Series calculates the covariance of a Series with other Series, ignoring missing values. The computation automatically excludes both NA and null values.

Syntax:

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

Parameters

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

min_periods: This is optional. It is an int that indicates the minimum number of observations required in order for a valid result to be obtained.

ddof: This is optional. Delta Degrees of Freedom is indicated by this argument. N – ddof is the divisor used in calculations, where N is the number of elements.

Return Value:

The covariance of a Series with other Series is returned by the cov() function of the Pandas Series.

Pandas Series cov() Function in Python

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
  • Pass the given second series as an argument to the cov() function and apply it on the given first series to get the covariance 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()
# Pass the given second series as an argument to the cov() function and apply it 
# on the given first series to get the covariance of the first series with the second
# and print the result.
print("The covariance of first series with the second:")
print(gvn_series1.cov(gvn_series2))

Output:

The 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 covariance of first series with the second:
2.989642857142857

Example2

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 cov() function on the student_rollno and student_marks column of the dataframe to get the covariance 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 cov() function on the student_rollno and student_marks column of the 
# dataframe to get the covariance of student_rollno column of the dataframe 
# with the student_marks and print the result.
print("The covariance of student_rollno column of the dataframe with the student_marks:")
print(data_frme['student_rollno'].cov(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 covariance of student_rollno column of the dataframe with the student_marks:
3.333333333333333

 

Leave a Comment