- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Correlation between two numeric columns in a Pandas DataFrame
We can use pandas.DataFrame.corr to compute pairwise correlation of columns, excluding NULL values. The correlation coefficient indicates the strength of the linear association between two variables. The coefficient ranges between -1 and 1.
To get the correlation between two numeric columns in a Pandas dataframe, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Create a Pandas dataframe of two-dimensional, size-mutable, potentially heterogeneous tabular data.
- Compare the values of the two columns and compute the correlation coefficient using col1.corr(col2).
- Print the correlation coefficient on the console.
- To display the figure, use show() method.
Example
import pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({'lab': [1, 2, 3], 'value': [3, 4, 5]}) col1 = df['lab'] col2 = df['value'] plt.plot(col1, col2) print("The correlation coefficient is: ", col1.corr(col2)) plt.show()
Output
It will produce the following output
The correlation coefficient is: 1.0
Here, the correlation coefficient is 1.0 which indicates perfect correlation. Hence, we get a straight line because all the points lie along a straight line.
- Related Articles
- How to get the correlation between two columns in Pandas?
- Select multiple columns in a Pandas DataFrame
- How to get the mean of columns that contains numeric values of a dataframe in Pandas Python?
- How to find numeric columns in Pandas?
- Python - Grouping columns in Pandas Dataframe
- How to find the correlation for data frame having numeric and non-numeric columns in R?
- Python - Name columns explicitly in a Pandas DataFrame
- Python Pandas – Filter DataFrame between two dates
- Python Pandas - Plot multiple data columns in a DataFrame?
- Python Pandas - Query the columns of a DataFrame
- Python - Select multiple columns from a Pandas dataframe
- How to find the correlation between corresponding columns of two matrices in R?
- Python Pandas – Count the rows and columns in a DataFrame
- Select DataFrame rows between two index values in Python Pandas
- Python - Renaming the columns of Pandas DataFrame

Advertisements