title: Create a new pandas DataFrame Column with a groupby
body: <p>I have a dataframe and I'd like to group by a column value and then do a calculation to create a new column.  Below is the set up data:</p>
  <pre><code>import pandas as pd
  
  df = pd.DataFrame({
      'Red' : [1,2,3,4,5,6,7,8,9,10],
      'Groups':['A','B','A','A','B','C','B','C','B','C'],
      'Blue':[10,20,30,40,50,60,70,80,90,100]
  })
     
  df.groupby('Groups').apply(print)
  </code></pre>
  <p>What I want to do is create a 'TOTAL' column in the original dataframe.  If it is the first record of the group 'TOTAL' gets a zero otherwise TOTAL will get the ['Blue'] at index subtracted by ['Red'] at index-1.</p>
  <p>I tried to do this in a function below but it does not work.</p>
  <pre><code>def funct(group):
      count = 0
      lst = []
      for info in group:
          if count == 0:
              lst.append(0)
              count += 1
          else: 
              num = group.iloc[count]['Blue'] - group.iloc[count-1]['Red']
              lst.append(num)
              count += 1
      group['Total'] = lst
      return group
  
  df = df.join(df.groupby('Groups').apply(funct))
  </code></pre>
  <p>The code works for the first group but then errors out.</p>
  <p>The desired outcome is:</p>
  <pre><code>df_final = pd.DataFrame({
      'Red' : [1,2,3,4,5,6,7,8,9,10],
      'Groups':['A','B','A','A','B','C','B','C','B','C'],
      'Blue':[10,20,30,40,50,60,70,80,90,100],
      'Total':[0,0,29,37,48,0,65,74,83,92]
  })
  
  df_final
  
  df_final.groupby('Groups').apply(print)
  </code></pre>
  <p>Thank you for the help!</p>