title: Populating a “count matrix” with combinations of pandas DataFrame rows
body: <p>Let's say I have the following pandas DataFrame in Python3.x</p>
  
  <pre><code>import pandas as pd
  
  dict1 = {'name':['dog', 'dog', 'cat', 'cat', 'cat', 'bird', 'bird', 'bird', 'bird'], 'number':[42, 42, 42, 42, 42, 42, 42, 42, 42], 'count':[1, 2, 4, 5, 7, 1, 2, 5, 8]} 
  df = pd.DataFrame(dict1)
  
  print(df)
  ##    name  number  count
  ## 0   dog      42      1
  ## 1   dog      42      2
  ## 2   cat      42      4
  ## 3   cat      42      5
  ## 4   cat      42      7
  ## 5  bird      42      1
  ## 6  bird      42      2
  ## 7  bird      42      5
  ## 8  bird      42      8
  </code></pre>
  
  <p>Column <code>counts</code> contains integers from 1 to 8. My goal is to populate an 8 by 8 zero matrix with the count of each combination "pair" given the unique category in column <code>name</code>. </p>
  
  <p>So, the combination pairs for <code>dog</code>, <code>cat</code>, and <code>bird</code> are:</p>
  
  <pre><code>dog: (1, 2)
  cat: (4, 5), (4, 7), (5, 7)
  bird: (1, 2), (1, 5), (1, 8), (2, 5), (2, 8), (5, 8)
  </code></pre>
  
  <p>For each pair, I add <code>+1</code> to the corresponding entry in the zero matrix. </p>
  
  <p>This matrix will be symmetric, i.e. <code>(n, m) = (m, n)</code>. The matrix given <code>df</code> would be:</p>
  
  <pre><code>   1 2 3 4 5 6 7 8
  1: 0 2 0 0 1 0 0 1
  2: 2 0 0 0 1 0 0 1
  3: 0 0 0 0 0 0 0 0
  4: 0 0 0 0 1 0 1 0
  5: 1 1 0 1 0 0 1 1
  6: 0 0 0 0 0 0 0 0
  7: 0 0 0 1 1 0 0 0
  8: 1 1 0 0 1 0 0 0
  </code></pre>
  
  <p>Note that <code>(1,2)=(2,1)</code> has a count 2, from the <code>dog</code> combination and the <code>bird</code> combination. </p>
  
  <p>(1) In order to do this, I think it would be best to create a list of "combinations tuples" given the pandas DataFrame. </p>
  
  <p>That is, something like</p>
  
  <pre><code>list_combos = [(1, 2), (2, 1), (4, 5), (4, 7), (5, 7), (5, 4), (7, 4), (7, 5),
      (1, 2), (1, 5), (1, 8), (2, 5), (2, 8), (5, 8), (2, 1), (5, 1),
      (8, 1), (5, 2), (8, 2), (8, 5)]
  </code></pre>
  
  <p>Given that the matrix is symmetric, perhaps it would be better to use:</p>
  
  <pre><code>list_combos2 = [(1, 2), (4, 5), (4, 7), (5, 7), (1, 2), (1, 5), (1, 8), (2, 5), (2, 8), (5, 8)]
  </code></pre>
  
  <p>How could one calculate the permutations of entires in a pandas DataFrame, given the categorical value in 'names'? </p>
  
  <p>(2) What would be the most algorithmically efficient (i.e. RAM) to populate this matrix, given the list of tuples? </p>
  
  <p>I should be able to feed a list of tuples into a numpy array, but how does one fill in the zeros? </p>