Pandas DataFrame corr() Method

Rate this post


Before any data manipulation can occur, two (2) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install numpy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.

Feel free to view the PyCharm installation guide for the required libraries.

Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import numpy as np 

DataFrame corr()

The corr() method computes pair-wise correlation of columns. This does not include NaN and NULL values.

The syntax for this method is as follows:

DataFrame.corr(method='pearson', min_periods=1)
method The possible correlation methods are:
'pearson': standard correlation coefficient. By default, Pearson.
'kendall': Kendall Tau correlation coefficient.
'spearman': Spearman rank correlation.
– Callable with two (2) 1D ndarrays and returns a float.
min_periodsThe minimum number of observations required per pair of columns to have a valid result. This option is only available for the Pearson and Spearman correlations.
df_prices = pd.DataFrame({'Tops':    [10.22, 12.45, 17.45],
                          'Tanks':   [9.99, 10.99, 11.99],
                          'Pants':   [24.95, 26.95, 32.95],
                          'Sweats':  [18.99, 19.99, 21.99]})

result = df_prices.corr()
  • Line [1] creates a DataFrame from a Dictionary of Lists and saves it to df_inv.
  • Line [2] applies the correlation method. The output saves to the result variable.
  • Line [3] outputs the result to the terminal.


Pants0.9979950.960769 1.0000000.995871

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.