pandas correlation matrix between one column and all others 1

pandas correlation matrix between one column and all others

correlations = df.corr().unstack().sort_values(ascending=False) # Build correlation matrix
correlations = pd.DataFrame(correlations).reset_index() # Convert to dataframe
correlations.columns = ['col1', 'col2', 'correlation'] # Label it
correlations.query("col1 == 'v2' & col2 != 'v2'") # Filter by variable
# output of this code will give correlation of column v2 with all the other columns 

Here is what the above code is Doing:
1. We’re using the corr() method to build a correlation matrix.
2. We’re using the unstack() method to reshape the correlation matrix so that we can filter by variable.
3. We’re sorting the values in descending order.
4. We’re converting the correlation matrix to a dataframe.
5. We’re labeling the columns.
6. We’re filtering the dataframe by variable.

Similar Posts