Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data preprocessing
  • Data Manipulation
  • Data Analysis using Pandas
  • EDA
  • Pandas Exercise
  • Pandas AI
  • Numpy
  • Matplotlib
  • Plotly
  • Data Analysis
  • Machine Learning
  • Data science
Open In App
Next Article:
Plotting Correlation Matrix using Python
Next article icon

How to Create a Correlation Matrix using Pandas?

Last Updated : 08 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Correlation is a statistical technique that shows how two variables are related. Pandas dataframe.corr() method is used for creating the correlation matrix. It is used to find the pairwise correlation of all columns in the dataframe. Any na values are automatically excluded. For any non-numeric data type columns in the dataframe it is ignored.
To create correlation matrix using pandas, these steps should be taken: 
 

  1. Obtain the data.
  2. Create the DataFrame using Pandas.
  3. Create correlation matrix using Pandas


Example 1: 
 

Python3

# import pandas
import pandas as pd
 
# obtaining the data
data = {'A': [45, 37, 42],
        'B': [38, 31, 26],
        'C': [10, 15, 17]
        }
# creation of DataFrame
df = pd.DataFrame(data)
 
# creation of correlation matrix
corrM = df.corr()
 
corrM
                      
                       

Output:
 

pandas-matrix-1


 

Values at the diagonal shows the correlation of a variable with itself, hence diagonal shows the correlation 1.


Example 2: 
 

Python3

import pandas as pd
 
data = {'A': [45, 37, 42, 50],
        'B': [38, 31, 26, 90],
        'C': [10, 15, 17, 100],
        'D': [60, 99, 23, 56],
        'E': [76, 98, 78, 90]
        }
 
df = pd.DataFrame(data)
 
corrM = df.corr()
corrM
                      
                       

Output: 
 

pandas-matrix-2


Example 3: 
 

Python3

import pandas as pd
 
# Integer and string values can
# never be correlated.
data = {'A': [45, 37, 42, 50],
        'B': ['R', 'O', 'M', 'Y'],
        }
 
df = pd.DataFrame(data)
 
corrM = df.corr()
corrM
                      
                       

Output: 
 

python-matrix-3


Example 4: 
 

Python3

import pandas as pd
 
data = {'A': [45, 37, 42, 50],
        'B': ['R', 'O', 'M', 'Y'],
        'C': [56, 67, 68, 60],
               
        }
 
df = pd.DataFrame(data)
 
corrM = df.corr()
corrM
                      
                       

Output: 
 

python-matrix-5


 



Next Article
Plotting Correlation Matrix using Python
author
romy421kumari
Improve
Article Tags :
  • Python
  • Python pandas-dataFrame
  • Python-pandas
Practice Tags :
  • python

Similar Reads

  • Create a correlation Matrix using Python
    A Correlation matrix is a table that shows how different variables are related to each other. Each cell in the table displays a number i.e. correlation coefficient which tells us how strongly two variables are together. It helps in quickly spotting patterns, understand relationships and making bette
    3 min read
  • How to create a correlation heatmap in Python?
    Seaborn is a powerful Python library based on Matplotlib, designed for data visualization. It provides an intuitive way to represent data using statistical graphics. One such visualization is a heatmap, which is used to display data variation through a color palette. In this article, we focus on cor
    3 min read
  • Convert covariance matrix to correlation matrix using Python
    In this article, we will be discussing the relationship between Covariance and Correlation and program our own function for calculating covariance and correlation using python.  Covariance: It tells us how two quantities are related to one another say we want to calculate the covariance between x an
    5 min read
  • Plotting Correlation Matrix using Python
    Correlation means an association, It is a measure of the extent to which two variables are related.  1. Positive Correlation: When two variables increase together and decrease together. They are positively correlated. '1' is a perfect positive correlation. For example - demand and profit are positiv
    3 min read
  • How to create a Triangle Correlation Heatmap in seaborn - Python?
    Seaborn is a Python library that is based on matplotlib and is used for data visualization. It provides a medium to present data in a statistical graph format as an informative and attractive medium to impart some information. A heatmap is one of the components supported by seaborn where variation i
    4 min read
  • Calculate Cramér's Coefficient Matrix Using Pandas
    In statistics, understanding relationships between categorical variables is crucial. One such tool for measuring association between two categorical variables is Cramer's V, an extension of the chi-square test. Unlike correlation, which is used for continuous data, Cramer's V is specifically designe
    4 min read
  • How to Calculate Autocorrelation in Python?
    Correlation generally determines the relationship between two variables. Correlation is calculated between the variable and itself at previous time steps, such a correlation is called Autocorrelation. Method 1 : Using lagplot() The daily minimum temperatures dataset is used for this example. As the
    3 min read
  • Using pandas crosstab to create a bar plot
    In this article, we will discuss how to create a bar plot by using pandas crosstab in Python. First Lets us know more about the crosstab, It is a simple cross-tabulation of two or more variables. What is cross-tabulation? It is a simple cross-tabulation that help us to understand the relationship be
    3 min read
  • How to Plot a Dataframe using Pandas
    Pandas plotting is an interface to Matplotlib, that allows to generate high-quality plots directly from a DataFrame or Series. The .plot() method is the core function for plotting data in Pandas. Depending on the kind of plot we want to create, we can specify various parameters such as plot type (ki
    8 min read
  • How to Join Pandas DataFrames using Merge?
    Joining and merging DataFrames is that the core process to start  out with data analysis and machine learning tasks. It's one of the toolkits which each Data Analyst or Data Scientist should master because in most cases data comes from multiple sources and files. In this tutorial, you'll how to join
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences