title: How would I do this complex pivot transformation in Pandas
body: <p>I'd like to access a sequence of data within a group as new columns of a flattened dataframe. One problem is that the sequence length varies, so some columns will be null. Here's an example of a dummy example. df is the original dataframe, and transformed_df is what I'd like it to look like.</p>
  <pre><code>time = ['1:00 pm','7:00 pm','8:00 am','2:00 pm','2:00 pm','10:00 pm','10:00 am']
  people = ['mike']*4 + ['bob']*3
  symptom = ['sore throat day','cough night','sore throat day','cough afternoon',
            'sneeze day','insomnia night','fatigue day']
  
  df = pd.DataFrame({'time':time,'person':people,'symptom':symptom})
  
  
  cols = ['person','first_symptom','second_symptom','third_symptom','fourth_symptom','time_between_second_and_first',
  'time_between_third_and_second','time_between_fourth_and_third']
  
  transformed_df = pd.DataFrame(columns = cols)
  
  row1 = ['mike','sore throat day','cough night','sore throat day','cough afternoon',
          6,13,6]
  row2 = ['bob','sneeze day','insomnia night','fatigue day',np.nan,8,12,np.nan]
  
  transformed_df = transformed_df.append(dict(zip(cols,row1)),ignore_index=True)
  transformed_df = transformed_df.append(dict(zip(cols,row2)),ignore_index=True)
  
  </code></pre>
  <p><a href="https://i.stack.imgur.com/WZy5l.jpg" rel="nofollow noreferrer">original df</a> <br/>
  <a href="https://i.stack.imgur.com/q047F.jpg" rel="nofollow noreferrer">transformed df</a></p>
  <p>So essentially I'm grouping by the person column, extracting out their sequential symptoms as new columns, and also extracting the time delta between symptoms as new columns. Is there a way to do this in Pandas?</p>
  <p>Thanks so much and have a wonderful day.</p>