title: convert from table to matrix in python
body: <p>I have the following data in form of pandas data frame (df):</p>
  
  <p>Each column name has starting and ending nodes separated by '_'. I.e, 'A_B' means a starting node of A and ending node of B</p>
  
  <pre><code>A_A   A_B   A_C   B_B   B_A   B_C   C_C   C_A   C_B
  12    23    33    43    19    98    44    77    35
  </code></pre>
  
  <p>and I want to convert it into a a 2D matrix that gets output to a csv so that it looks like:</p>
  
  <pre><code>   A    B    C
  A  12   23   33
  B  19   43   98
  C  77   35   44
  </code></pre>
  
  <p>To do this, assume that we have </p>
  
  <pre><code>from itertools import groupby, combinations_with_replacement
  col_names = ['A','B','C']
  
  for i in combinations_with_replacement(col_names, 2):
      get_val = df[i[0]+'_'+i[1]]
  </code></pre>
  
  <p>Is there a way in pandas to output this as a matrix and also print out row and column names?</p>