title: Creating new matrix from dataframe and matrix in pandas
body: <p>I have a dataframe <code>df</code> which looks like this:</p>
  
  <pre><code>    id1  id2  weights
  0   a    2a   144.0
  1   a    2b   52.5
  2   a    2c   2.0
  3   a    2d   1.0
  4   a    2e   1.0
  5   b    2a   2.0
  6   b    2e   1.0
  7   b    2f   1.0
  8   b    2b   1.0
  9   b    2c   0.008
  </code></pre>
  
  <p>And a similarity matrix <code>mat</code> between the elements of the <code>id2</code> column:</p>
  
  <pre><code>    2a    2b   2c   2d   2e   2f
  2a  1     0.5  0.7  0.2  0.1  0.3
  2b  0.5   1    0.6  0.4  0.3  0.4
  2c  0.7   0.6  1    0.1  0.4  0.2
  2d  0.2   0.4  0.1  1    0.8  0.7
  2e  0.1   0.3  0.4  0.8  1    0.8
  2f  0.3   0.4  0.2  0.7  0.8  1
  </code></pre>
  
  <p>Now I would like to create a similarity matrix between the elements of <code>id1</code> and the elements from <code>id2</code>. For that I consider the elements of <code>id1</code> as barycentres of the corresponding elements of <code>id2</code> ind my dataframe <code>df</code> (with the corresponding <code>weights</code>).</p>
  
  <p>My first attempt to do that is with loops (aouch):</p>
  
  <pre><code>ids = df.id1.unique()
  output = pd.DataFrame(columns = mat.columns,index = ids)
  for id in ids:
      df_slice = df.loc[df.id1 == id]
      to_normalize = df_slice.weights.sum()
      temp = mat.loc[df_slice.id2]
      for art in df_slice.id2:
          temp.loc[art] *= df_slice.ix[df_slice.id2 == art,'weights'].values[0]
          temp.loc[art] /= (1.*to_normalize)
      output.loc[id] = temp.sum()
  </code></pre>
  
  <p>But of course this is way not pythonic, and takes ages (<code>timeit</code> for these small matrix showed <code>21.3ms</code> not computable for a 10k-rows <code>df</code> and 3k by 3k <code>mat</code>). What would be a more clean/efficient way to do it? </p>
  
  <p>Desired output:</p>
  
  <pre><code>    2a          2b          2c          2d          2e          2f
  a   0.857606    0.630424    0.672319    0.258354    0.163342    0.329676
  b   0.580192    0.540096    0.520767    0.459425    0.459904    0.559425
  </code></pre>
  
  <p>And is there a way to compute another similarity matrix between the elements of <code>id1</code> (from this data)?</p>
  
  <p>Thank you in advance.</p>