title: Returning the highest and lowest correlations from a correlation matrix in pandas
body: <p>I have a bunch of stock data, and I am trying to build a dataframe that takes the top two, and bottom stocks from a correlation matrix, and also their actual correlation.</p>
  
  <p>Let's say the matrix, <code>corr</code> looks like this:</p>
  
  <pre><code>  A    B    C    D    E
  A 1.00 0.65 0.31 0.94 0.55
  B 0.87 1.00 0.96 0.67 0.41
  C 0.95 0.88 1.00 0.72 0.69
  D 0.64 0.84 0.99 1.00 0.78
  E 0.71 0.62 0.89 0.32 1.00
  </code></pre>
  
  <p>What I want to do is to be able to is return the best two, and least correlated stocks, and their correlation, for Stock A, B, C, D &amp; E, while dropping the obvious 1.00 correlation each stock has to itself.</p>
  
  <p>The resulting dataframe, or whatever is easiest to display this is would look like this:</p>
  
  <pre><code>Stock 1st 1st_Val 2nd 2nd_Val Last Last_Val
  A     D   0.94    B   0.65    C    0.31
  B     C   0.96    A   0.87    E    0.41
  C     A   0.95    B   0.88    E    0.69
  D     C   0.99    B   0.84    A    0.64
  E     C   0.89    A   0.71    D    0.32
  </code></pre>
  
  <p>With my attempts so far I have been able to look through and return the relevant stock names using <code>corr[stock].nlargest().index[0:].tolist()</code>, and then taking <code>[1]</code>, <code>[2]</code> and <code>[-1]</code> from each list and sticking them in a dictionary and building the dataframe from there. But I'm unable to return the correlation value and I suspect I'm not doing this in the most efficient way anyway.</p>
  
  <p>Any help really appreciated, cheers</p>