title: Pandas - subtracting 2 variable after group by
body: <p>I have the following Pandas "code"</p>
  
  <pre><code>df=pd.read_csv('option_data.csv')
  
  In [30]:
  
  df.head(9)
  ​
  Out[30]:
       S    date    E   D     V
  0  IBM  1/2/2008  1  10  0.1718
  1  IBM  1/2/2008  1  50  0.2144
  2  IBM  1/2/2008  1  90  0.2733
  3  IBM  1/3/2008  1  10  0.1692
  4  IBM  1/3/2008  1  50  0.2081
  5  IBM  1/3/2008  1  90  0.2634
  6  IBM  1/4/2008  1  10  0.1844
  7  IBM  1/4/2008  1  50  0.2283
  8  IBM  1/4/2008  1  90  0.2779
  </code></pre>
  
  <p>I wish to create a variable that is the spread between V when D = 90 and V when D=10.  The first value would be (.2733 - .1718) = .1015.  Example below:</p>
  
  <pre><code>S   date        E   D      V    Skew
  IBM 1/2/2008    1   10  0.1718  
  IBM 1/2/2008    1   50  0.2144  0.1015
  IBM 1/2/2008    1   90  0.2733  
  IBM 1/3/2008    1   10  0.1692  
  IBM 1/3/2008    1   50  0.2081  0.0942
  IBM 1/3/2008    1   90  0.2634  
  IBM 1/4/2008    1   10  0.1844  
  IBM 1/4/2008    1   50  0.2283  0.0935
  IBM 1/4/2008    1   90  0.2779
  </code></pre>
  
  <p>the last variable is the change in the "Skew variable" as a percent of V when D=50 for the first Skew number.  So, (0.0942-0.1015)/0.2144 = -.03405</p>
  
  <pre><code>S   date        E   D   V       Skew    pct_change_Skew
  IBM 1/2/2008    1   10  0.1718      
  IBM 1/2/2008    1   50  0.2144  0.1015  
  IBM 1/2/2008    1   90  0.2733      
  IBM 1/3/2008    1   10  0.1692      
  IBM 1/3/2008    1   50  0.2081  0.0942  -0.03405
  IBM 1/3/2008    1   90  0.2634      
  IBM 1/4/2008    1   10  0.1844      
  IBM 1/4/2008    1   50  0.2283  0.0935  -0.00001
  IBM 1/4/2008    1   90  0.2779      
  </code></pre>
  
  <p>My setup is  below:</p>
  
  <pre><code>df = df.groupby(['S','date','E']).apply(????)
  </code></pre>
  
  <p>I need to perform the groupby as there are many S (symbols), dates and E values.</p>
  
  <p>Ideally I would have the new variables populate each row as such:</p>
  
  <pre><code>S   date        E   D     V      Skew   pct_change_Skew
  IBM 1/2/2008    1   10  0.1718  0.1015  
  IBM 1/2/2008    1   50  0.2144  0.1015  
  IBM 1/2/2008    1   90  0.2733  0.1015  
  IBM 1/3/2008    1   10  0.1692  0.0942  -0.03405
  IBM 1/3/2008    1   50  0.2081  0.0942  -0.03405
  IBM 1/3/2008    1   90  0.2634  0.0942  -0.03405
  IBM 1/4/2008    1   10  0.1844  0.0935  -0.00001
  IBM 1/4/2008    1   50  0.2283  0.0935  -0.00001
  IBM 1/4/2008    1   90  0.2779  0.0935  -0.00001
  </code></pre>
  
  <p>If I can be so bold as to ask for a pandas as well as a python "straight" code example, it would help my learning curve.  Thank you very much for any assistance in this.</p>
  
  <p>John</p>