title: Pivot dataframe with columns constant withing the index column
body: <p>Suppose I have the following dataframe, where both <code>Y</code> and <code>Z</code> are constant within <code>ID</code>:</p>
  
  <pre><code>   ID TYPE  X    Y   Z
  0   1    A  1  foo  10
  1   1    B  2  foo  10
  2   2    A  3  bar  20
  3   2    B  4  bar  20
  4   3    A  5  baz  30
  5   3    B  6  baz  30
  </code></pre>
  
  <p>I would like to reshape the data from a "long" to "wide" format:</p>
  
  <pre><code>   ID  XA  XB    Y   Z
  0   1   1   2  foo  10
  1   2   3   4  bar  20
  2   3   5   6  baz  30
  </code></pre>
  
  <p>However, if I use <code>pandas.DataFrame.pivot()</code>:</p>
  
  <pre><code>df_new = df.pivot(index='ID', columns='TYPE')
  </code></pre>
  
  <p>I will get duplicates of <code>Y</code> and <code>Z</code>:</p>
  
  <pre><code>      X       Y        Z    
  TYPE  A  B    A    B   A   B
  ID                          
  1     1  2  foo  foo  10  10
  2     3  4  bar  bar  20  20
  3     5  6  baz  baz  30  30
  </code></pre>
  
  <p>To get the desired output, I could do the following:</p>
  
  <pre><code>import pandas as pd
  
  df = pd.DataFrame({'ID': [1, 1, 2, 2, 3, 3],
                     'TYPE': ['A', 'B', 'A', 'B', 'A', 'B'],
                     'X': [1, 2, 3, 4, 5, 6],
                     'Y': ['foo', 'foo', 'bar', 'bar', 'baz', 'baz'],
                     'Z': [10, 10, 20, 20, 30, 30]})
  
  
  def long_to_wide(df, i, j, varlist):
      df_wide = df.pivot(index='ID', columns='TYPE')
      df_wide.columns = [''.join(col).strip() for col in df_wide.columns.values]
      df_wide.reset_index(inplace=True)
  
      for var in varlist:
          if pd.Series.equals(df_wide[var + 'A'], df_wide[var + 'B']):
              df_wide.drop((var + 'B'), axis = 1, inplace = True)
          else:
              raise
              # Error handling of some sort...
          df_wide = df_wide.rename(columns={var + 'A': var})
  
      return df_wide
  
  
  df_new = long_to_wide(df, 'ID', 'TYPE', ['Y', 'Z'])
  </code></pre>
  
  <p>However, I feel that this must be unnecessarily complicated. For example, to get the desired output in Stata, one could run either: </p>
  
  <pre><code>reshape wide X, i(ID) j(TYPE)
  </code></pre>
  
  <p>or</p>
  
  <pre><code>reshape wide X, i(ID Y Z) j(TYPE)
  </code></pre>
  
  <p>This situation is quite common and I therefore thought there should be a built-in method to handle it. But after looking around at the <code>Pandas</code> documentation and also here at Stack Overflow, I haven't found a simpler solution. </p>
  
  <p>Is there one?</p>