- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# How to get the correlation between two columns in Pandas?

We can use the **.corr()** method to get the correlation between two columns in Pandas. Let's take an example and see how to apply this method.

## Steps

- Create a two-dimensional, size-mutable, potentially heterogeneous tabular data,
**df**. - Print the input DataFrame,
**df**. - Initialize two variables,
**col1**and**col2**, and assign them the columns that you want to find the correlation of. - Find the correlation between
**col1**and col2 by using df[col1].corr(df[col2]) and save the correlation value in a variable, corr. - Print the correlation value, corr.

## Example

import pandas as pd df = pd.DataFrame( { "x": [5, 2, 7, 0], "y": [4, 7, 5, 1], "z": [9, 3, 5, 1] } ) print "Input DataFrame is:\n", df col1, col2 = "x", "y" corr = df[col1].corr(df[col2]) print "Correlation between ", col1, " and ", col2, "is: ", round(corr, 2) col1, col2 = "x", "x" corr = df[col1].corr(df[col2]) print "Correlation between ", col1, " and ", col2, "is: ", round(corr, 2) col1, col2 = "x", "z" corr = df[col1].corr(df[col2]) print "Correlation between ", col1, " and ", col2, "is: ", round(corr, 2) col1, col2 = "y", "x" corr = df[col1].corr(df[col2]) print "Correlation between ", col1, " and ", col2, "is: ", round(corr, 2)

## Output

Input DataFrame is: x y z 0 5 4 9 1 2 7 3 2 7 5 5 3 0 1 1 Correlation between x and y is: 0.41 Correlation between x and x is: 1.0 Correlation between x and z is: 0.72 Correlation between y and x is: 0.41

- Related Articles
- Correlation between two numeric columns in a Pandas DataFrame
- How to find the correlation between corresponding columns of two matrices in R?
- How to get the difference between two columns in a new column in MySQL?
- How to find the correlation coefficient between two data frames in R?
- Python – Get the Columns Shared by Two Pandas DataFrames using Numpy
- Python - Fetch columns between two Pandas DataFrames by Intersection
- How to find the correlation coefficient between rows of two data frames in R?
- How to get the greatest of two columns values in MySQL?
- Python Pandas – Can we use & Operator to find common columns between two DataFrames?
- Python Pandas – Get the datatype and DataFrame columns information
- How to find numeric columns in Pandas?
- How to plot two columns of a Pandas data frame using points?
- Python - How to Concatenate Two or More Pandas DataFrames along columns?\n
- How to get the difference between two dates in Android?
- How to get the difference between two arrays in JavaScript?

Advertisements