title: calculation using shift and groupby
body: <p>I would like to calculate a new column &quot;change&quot;. <br>
  The value of the new column shall be calculated as follows: <br>
  X / Z (one cell above)
  --&gt; first row would be empty.<br>
  --&gt; second row would be 1 / 6 = 0,16<br />
  -&gt; second row would be 5 / 10 = 0,5 ...and so on..</p>
  <pre><code>df = pd.DataFrame({'id': ['a', 'a', 'a', 'b', 'b','b'],
                      'Z': [6,10,8, 6, 6,15],
                       'X': [2,1,5, 2, 3,20]})
  var =  df.columns[1]
  </code></pre>
  <p><a href="https://i.stack.imgur.com/8DJKJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8DJKJ.png" alt="df" /></a></p>
  <p>In the forum I found this:<br>
  <code> df['change'] = df['X'] / df[var].shift(1)</code>
  It works fine. But I needed to insert a &quot;groupby&quot; argument and cant get it to work.
  I tried this:<br>
  <code>df['change'] = df.groupby('id').apply(lambda x: x['X'] / x[var].shift(1))</code><br>
  But I get an error:<br>
  <em>&quot;incompatible index of inserted column with frame index&quot;</em></p>
  <p>I am afraid I have not fully understood this lambda function.
  Any ideas how to get that right?
  Thanks in advance!</p>