Create a Covariance Matrix in Python

Your goal

You need to calculate the covariance matrix for a set of numerical variables in Python. Your variables are stored in a Pandas DataFrame.

Step-by-step tutorial

In Python, you can use either Pandas or NumPy to generate covariance matrices. We'll use Pandas since we're already assuming a Pandas DataFrame.

>>> import pandas as pd
>>> precip = pd.read_csv("precip-central-park.csv")
>>> precip
     YEAR   JAN   FEB   MAR   APR   MAY   JUN   JUL   AUG   SEP   OCT   NOV   DEC  ANNUAL
0    1869  2.53  6.87  4.61  1.39  4.15  4.40  3.20  1.76  2.81  6.48  2.03  5.02   45.25
1    1870  4.41  2.83  3.33  5.11  1.83  2.82  3.76  3.07  2.52  4.97  2.42  2.18   39.25
2    1871  2.07  2.72  5.54  3.03  4.04  7.05  5.57  5.60  2.34  7.50  3.56  2.24   51.26
3    1872  1.88  1.29  3.74  2.29  2.68  2.93  7.83  6.29  2.95  3.35  4.08  3.18   42.49
4    1873  5.34  3.80  2.09  4.16  3.69  1.28  4.61  9.56  3.14  2.73  4.63  2.96   47.99
..    ...   ...   ...   ...   ...   ...   ...   ...   ...   ...   ...   ...   ...     ...
146  2015  5.23  2.04  4.72  2.08  1.86  4.79  3.98  2.35  3.28  3.91  2.01  4.72   40.97
147  2016  4.41  4.40  1.17  1.61  3.75  2.60  7.02  1.97  2.79  4.15  5.41  2.89   42.17
148  2017  4.83  2.48  5.25  3.84  6.38  4.76  4.19  3.34  2.00  4.18  1.58  2.21   45.04
149  2018  2.18  5.83  5.17  5.78  3.53  3.11  7.45  8.59  6.19  3.59  7.62  6.51   65.55
150  2019  3.58  3.14  3.87  4.55  6.82  5.46  5.77  3.70  0.95  6.15  1.95  7.09   53.03

[151 rows x 14 columns]
>>> precip.cov()
               YEAR       JAN       FEB       MAR  ...       OCT        NOV        DEC      ANNUAL
YEAR    1912.666667  2.178000 -6.418933  9.020667  ...  7.637667  13.138200  16.526200  106.995133
JAN        2.178000  2.664147 -0.161606 -0.310848  ...  0.145606  -0.406393   0.100035    1.869489
FEB       -6.418933 -0.161606  2.106540 -0.067497  ...  0.099883  -0.168084   0.119883    1.506022
MAR        9.020667 -0.310848 -0.067497  3.443700  ...  0.692109  -0.103777   0.433878    4.956755
APR       16.597467  0.353743 -0.234048  0.783675  ...  0.862752  -0.050458   0.884854    7.573764
MAY       20.134467  0.097580  0.062869 -0.080136  ... -0.402771   0.037876   0.503115    5.466202
JUN       17.304800 -0.280197  0.169104  0.100356  ...  0.683594   0.075521   0.501449    6.281717
JUL        0.714533 -0.536097 -0.220406 -0.233786  ... -0.243258   0.571658   0.338517    5.683621
AUG        0.973267  0.254137 -0.049978  0.209051  ...  0.820867  -0.131071   0.214891    9.581224
SEP        9.222200 -0.050359 -0.149665  0.088357  ... -0.477822  -0.307715  -0.157210    6.720850
OCT        7.637667  0.145606  0.099883  0.692109  ...  6.386653   0.258304   0.361118    9.184290
NOV       13.138200 -0.406393 -0.168084 -0.103777  ...  0.258304   4.384482  -0.110566    4.045168
DEC       16.526200  0.100035  0.119883  0.433878  ...  0.361118  -0.110566   3.107052    6.293792
ANNUAL   106.995133  1.869489  1.506022  4.956755  ...  9.184290   4.045168   6.293792   69.177980

[14 rows x 14 columns]