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")