• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / pandas covariance – Calculate Covariance Matrix Using cov() Function

pandas covariance – Calculate Covariance Matrix Using cov() Function

January 11, 2022 Leave a Comment

To find the covariance between columns in a DataFrame or Series in pandas, the easiest way is to use the pandas cov() function.

df.cov()

You can also use the numpy cov() function to calculate the covariance between two Series.

s1.cov(s2)

Finding the covariance between columns or Series using pandas is easy. We can use the pandas cov() function to find the covariance estimates of columns of numbers, or the covariance between multiple Series.

Let’s say we have the following DataFrame.

df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
                   'Weight': [130.54, 160.20, 209.45, 150.35, 117.73, 187.52],
                   'Height': [50.10, 68.94, 71.42, 48.56, 59.37, 63.42],
                   'Age': [43,23,71,49,52,37] })

print(df)
# Output: 
    Name  Weight  Height  Age
0    Jim  130.54   50.10   43
1  Sally  160.20   68.94   23
2    Bob  209.45   71.42   71
3    Sue  150.35   48.56   49
4   Jill  117.73   59.37   52
5  Larry  187.52   63.42   37

To get the covariance matrix between the numeric columns, we can use the pandas cov() function in the following Python code:

print(df.cov())

# Output:
             Weight      Height         Age
Weight  1189.501177  218.115103  157.815667
Height   218.115103   90.154177    8.200333
Age      157.815667    8.200333  257.766667

Calculating Covariance between Series in pandas

We can also use the numpy cov() function to find the covariance between Series using pandas.

Let’s say we have the same DataFrame from the example in the first section of this article.

To compute the covariance using the numpy cov() function, we just need to create two Series from the DataFrame and then call the function.

s1 = pd.Series(df["Weight"])
s2 = pd.Series(df["Age"])
print(s1.cov(s2))

# Output:
157.8156666666667

As you can see, this is the same covariance estimate we saw in the first example for the columns “Weight” and “Age”.

Hopefully this article has been helpful for you to understand how to compute covariance for columns in a DataFrame or Series using pandas.

Other Articles You'll Also Like:

  • 1.  Python tan – Find Tangent of Number in Radians Using math.tan()
  • 2.  How to Group By Columns and Find Sum in pandas DataFrame
  • 3.  Using Python to Split String into Dictionary
  • 4.  Read Pickle Files with pandas read_pickle Function
  • 5.  pandas mode – Find Mode of Series or Columns in DataFrame
  • 6.  Using Python to Sort Two Lists Together
  • 7.  Intersection of Two Lists in Python
  • 8.  PROC MIXED Equivalent in Python for Least Squared Means ANOVA
  • 9.  pi in Python – Using Math Module and Leibniz Formula to Get Value of pi
  • 10.  Using Python to Get Queue Size

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy