title: Need help on agg function after groupby for doing operation last - first
body: <p>I've below pandas dataframe.</p>
  <pre><code>  group   A   B   C   D   E
  0    g1  12  14  26  68  83
  1    g1  56  58  67  34  97
  2    g1  47  87  23  87  90
  3    g2  43  76  98  32  78
  4    g2  32  56  36  87  65
  5    g2  54  12  24  45  95
  </code></pre>
  <p>I wish to apply groupby on same using column 'group' and wish to apply aggregate function to get (last - first) for column 'E'.</p>
  <p>The expected output:</p>
  <pre><code>  group   A   B    C   D  E
  0    g1  12  87  116  34  7
  1    g2  43  12  158  32  17
  </code></pre>
  <p>I've written below code. But it is not working.</p>
  <pre><code>import pandas as pd
  
  df = pd.DataFrame([[&quot;g1&quot;, 12, 14, 26, 68, 83], [&quot;g1&quot;, 56, 58, 67, 34, 97], [&quot;g1&quot;, 47, 87, 23, 87, 90], [&quot;g2&quot;, 43, 76, 98, 32, 78], [&quot;g2&quot;, 32, 56, 36, 87, 65], [&quot;g2&quot;, 54, 12, 24, 45, 95]], columns=[&quot;group&quot;, &quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;])
  ndf = df.groupby([&quot;group&quot;], as_index=False).agg({&quot;A&quot;: 'first', &quot;B&quot;: 'last', &quot;C&quot;: 'sum', &quot;D&quot;: 'min', &quot;E&quot;: 'last - first'})
  print(df)
  print(ndf)
  </code></pre>