Pandas How To Uncategorized How to calculate correlation

# How to calculate correlation

In this post you learn how to calculate correlation between columns of your dataframe. In pandas, you can calculate the correlation between columns in a DataFrame by using the .corr() method.

## Example of correlation calculations

For example:

```import pandas as pd

df = pd.DataFrame({"col1": [1, 2, 3, 4], "col2": [10, 20, 30, 40]})
result = df.corr()

```

The resulting result will be a DataFrame containing the pairwise correlation between columns in the original DataFrame:

```          col1      col2
col1  1.000000  1.000000
col2  1.000000  1.000000

```

By default, .corr() calculates Pearson’s correlation, but you can also calculate other types of correlation, such as Spearman’s correlation, by passing method argument:

```result = df.corr(method="spearman")

```
Tags:

## How to iterate over rows the Pandas wayHow to iterate over rows the Pandas way

In this lesson I show you how to iterate over rows the Pandas way.

## How to join two dataframes with different sizeHow to join two dataframes with different size

In this post you will learn how to join two dataframes with different size in Pandas.

## How to plot a histogram in PandasHow to plot a histogram in Pandas

To plot a histogram in pandas, use the plot method on a pandas DataFrame or Series, and set kind to ‘hist’: