title: Constructing a co-occurrence matrix in python pandas
body: <p>I know how to do this in <a href="https://stackoverflow.com/questions/10622730/constructing-a-co-occurrence-matrix-from-dummycoded-observations-in-r">R</a>. But, is there any function in pandas that transforms a dataframe to an nxn co-occurrence matrix containing the counts of two aspects co-occurring. </p>
  
  <p>For example a matrix df:</p>
  
  <pre><code>import pandas as pd
  
  df = pd.DataFrame({'TFD' : ['AA', 'SL', 'BB', 'D0', 'Dk', 'FF'],
                      'Snack' : ['1', '0', '1', '1', '0', '0'],
                      'Trans' : ['1', '1', '1', '0', '0', '1'],
                      'Dop' : ['1', '0', '1', '0', '1', '1']}).set_index('TFD')
  
  print df
  
  &gt;&gt;&gt; 
      Dop Snack Trans
  TFD                
  AA    1     1     1
  SL    0     0     1
  BB    1     1     1
  D0    0     1     0
  Dk    1     0     0
  FF    1     0     1
  
  [6 rows x 3 columns]
  </code></pre>
  
  <p>would yield:</p>
  
  <pre><code>    Dop Snack Trans
  
  Dop   0     2     3
  Snack 2     0     2
  Trans 3     2     0
  </code></pre>
  
  <p>Since the matrix is mirrored on the diagonal I guess there would be a way to optimize code.</p>