# How to Create a Correlation Matrix using Pandas?

## How to Create a Correlation Matrix using Pandas?

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:

 `# 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:

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

Example 2:

 `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:

Example 3:

 `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:

Example 4:

 `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:

Last Updated on October 19, 2021 by admin

## How to Convert Strings to Floats in Pandas DataFrame?How to Convert Strings to Floats in Pandas DataFrame?

How to Convert Strings to Floats in Pandas DataFrame? In this article, we’ll look at

## Data Comparison and Selection in PandasData Comparison and Selection in Pandas

Python | Data Comparison and Selection in Pandas Python is a great language for doing

## Python – Scaling numbers column by column with PandasPython – Scaling numbers column by column with Pandas

Python – Scaling numbers column by column with Pandas Scaling numbers in machine learning is

## Pandas dataframe.cov()Pandas dataframe.cov()

Python | Pandas dataframe.cov() Python is a great language for doing data analysis, primarily because

## How to read all excel files under a directory as a Pandas DataFrame ?How to read all excel files under a directory as a Pandas DataFrame ?

How to read all excel files under a directory as a Pandas DataFrame ? In

## Pandas DataFrame.nlargest()Pandas DataFrame.nlargest()

Python Pandas DataFrame.nlargest() Python is a great language for doing data analysis, primarily because of

## How to change the Pandas datetime format in Python?How to change the Pandas datetime format in Python?

How to change the Pandas datetime format in Python? The date-time default format is “YYYY-MM-DD”.

## Data Wrangling in PythonData Wrangling in Python

Data Wrangling in Python Data Wrangling is the process of gathering, collecting, and transforming Raw