Creating a list from a correlation matrix in pandas

Viewed 424

I am trying to create a list (3 of the highest correlated selections) from a correlation matrix. Let's say I have the following matrix:

     A     B     C     D     E
A  1.00  0.15  0.57  0.11  0.98
B  0.59  1.00  0.32  0.24  0.54
C  0.96  0.65  1.00  0.22  0.67
D  0.72  0.33  0.78  1.00  0.92
E  0.88  0.94  0.61  0.48  1.00

So lets say I then sort the matrix to give me the most correlated according to column B, the matrix will now look like this:

     A     B     C     D     E
B  0.59  1.00  0.32  0.24  0.54
E  0.88  0.94  0.61  0.48  1.00
C  0.96  0.65  1.00  0.22  0.67
D  0.72  0.33  0.78  1.00  0.92
A  1.00  0.15  0.57  0.11  0.98

As you can see the matrix has been sorted to show me column B's most correlated counterparts. What I would then like is to be able to return the top 3 correlated letters in a list form, while missing out the top line (B), as this is obviously 1:1 correlated.

So I would like top_correlated = ['E', 'C', 'D'] for example, or I want my list to be that I mean.

As with all my posts, I'm aware that the etiquette is to at least attempt to show some effort with regards to code, but as usual I'm completely stumped, hence why I am posting here. Any help greatly appreciated.

1 Answers

Instead of sorting the entire DataFrame, you can call nlargest on your column, grab the index, and slice from the first element, since it should always be itself.

col = 'B'
df[col].nlargest(4).index[1:].tolist()

['E', 'C', 'D']
Related