• 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
  • VBA
  • 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.  rfind Python – Find Last Occurrence of Substring in String
  • 2.  Using Python to Read File Character by Character
  • 3.  Multiply Each Element in List by Scalar Value with Python
  • 4.  Python Replace Space with Underscore Using String replace() Function
  • 5.  Python Prepend to List with insert() Function
  • 6.  Shift Values in a List Using Python
  • 7.  How to Capitalize the First Letter of Every Word in Python
  • 8.  Python Split List into N Sublists
  • 9.  pandas Absolute Value – Get Absolute Values in a Series or DataFrame
  • 10.  Check if Word is Palindrome Using Recursion with Python

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy