title: Pandas: Pivot column to headers
body: <p>I'm trying to pivot the values in a column to column headers, but maintain the rest of the data.  Here's my full code, along with the closest I can get to what I'm looking for.  The only problem with this is that I can't figure out how to keep the <code>end</code> column:</p>
  
  <pre><code>import pandas as pd
  
  starts = pd.date_range(start = '1/1/2017', freq = '31d', periods = 4).tolist()
  ends = pd.date_range(start = '1/31/2017', freq = '31d', periods = 4).tolist()
  
  df = pd.DataFrame({ 'id':['XXX','XXX','XXX','XXX','YYY','YYY','YYY','YYY'], 
                      'start': starts + starts,
                      'end': ends + ends,
                      'type':['car','car','car','car','truck','truck','truck','truck']
                      }, columns = ['id','start','end','type'])
  </code></pre>
  
  <p><strong>Original Dataframe:</strong></p>
  
  <pre><code>    id      start        end   type
  0  XXX 2017-01-01 2017-01-31    car
  1  XXX 2017-02-01 2017-03-03    car
  2  XXX 2017-03-04 2017-04-03    car
  3  XXX 2017-04-04 2017-05-04    car
  4  YYY 2017-01-01 2017-01-31  truck
  5  YYY 2017-02-01 2017-03-03  truck
  6  YYY 2017-03-04 2017-04-03  truck
  7  YYY 2017-04-04 2017-05-04  truck
  </code></pre>
  
  <p><strong>My closest current pivot attempt:</strong></p>
  
  <pre><code>print df.pivot(index = 'start', columns = 'id', values = 'type').reset_index()
  </code></pre>
  
  <p><strong>Current output:</strong></p>
  
  <pre><code>id      start  XXX    YYY
  0  2017-01-01  car  truck
  1  2017-02-01  car  truck
  2  2017-03-04  car  truck
  3  2017-04-04  car  truck
  </code></pre>
  
  <p><strong>Desired output:</strong></p>
  
  <pre><code>        start         end  XXX    YYY
  0  2017-01-01  2017-01-31  car  truck
  1  2017-02-01  2017-03-03  car  truck
  2  2017-03-04  2017-04-03  car  truck
  3  2017-04-04  2017-05-04  car  truck
  </code></pre>
  
  <p>I've tried both <a href="https://stackoverflow.com/questions/26255671/pandas-column-values-to-columns">this</a> and <a href="https://stackoverflow.com/questions/22173572/change-column-values-to-column-headers-in-pandas">this</a>, with no success.</p>
  
  <p>Any help would be appreciated.</p>