title: Trouble pivoting in pandas (spread in R)
body: <p>I'm having some issues with the pd.pivot() or pivot_table() functions in pandas. </p>
  
  <p>I have this:</p>
  
  <pre><code>df = pd.DataFrame({'site_id': {0: 'a', 1: 'a', 2: 'b', 3: 'b', 4: 'c', 5:
   'c',6: 'a', 7: 'a', 8: 'b', 9: 'b', 10: 'c', 11: 'c'},
                     'dt': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1,6: 2, 7: 2, 8: 2, 9: 2, 10: 2, 11: 2},
                     'eu': {0: 'FGE', 1: 'WSH', 2: 'FGE', 3: 'WSH', 4: 'FGE', 5: 'WSH',6: 'FGE', 7: 'WSH', 8: 'FGE', 9: 'WSH', 10: 'FGE', 11: 'WSH'},
                     'kw': {0: '8', 1: '5', 2: '3', 3: '7', 4: '1', 5: '5',6: '2', 7: '3', 8: '5', 9: '7', 10: '2', 11: '5'}})
  
  
  df
  Out[140]: 
      dt   eu kw site_id
  0    1  FGE  8       a
  1    1  WSH  5       a
  2    1  FGE  3       b
  3    1  WSH  7       b
  4    1  FGE  1       c
  5    1  WSH  5       c
  6    2  FGE  2       a
  7    2  WSH  3       a
  8    2  FGE  5       b
  9    2  WSH  7       b
  10   2  FGE  2       c
  11   2  WSH  5       c
  </code></pre>
  
  <p>I want this:</p>
  
  <pre><code>dt   site_id   FGE   WSH
   1         a     8     5
   1         b     3     7
   1         c     1     5
   2         a     2     3
   2         b     5     7
   2         c     2     5
  </code></pre>
  
  <p>I've tried everything!</p>
  
  <pre><code>df.pivot_table(index = ['site_id','dt'], values = 'kw', columns = 'eu')
  </code></pre>
  
  <p>or</p>
  
  <pre><code>df.pivot(index = ['site_id','dt'], values = 'kw', columns = 'eu')
  </code></pre>
  
  <p>should have worked. I also tried unstack():</p>
  
  <pre><code>df.set_index(['dt','site_id','eu']).unstack(level = -1)
  </code></pre>