title: How to use a previous row value in a pandas dataframe when the previous value is also calculated witht group data
body: <p>I Have such DataFrame:</p>
  
  <p><a href="https://i.stack.imgur.com/RBtHx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RBtHx.png" alt="enter image description here"></a></p>
  
  <pre><code>df = pd.DataFrame({'id': [111,111,111, 222,222,222],\
                     'Date': ['30.04.2020', '31.05.2020', '30.06.2020', \
                              '30.04.2020', '31.05.2020', '30.06.2020'],\
                     'Debt': [100,100,70, 200,200,200] , \
                     'Ear_coef': [0,0.2,0.2, 0,0,0.3]}) 
  df['Date'] = pd.to_datetime(df['Date'] ) 
  df['Contract'] = pd.DataFrame(df.groupby(['id']).apply(lambda x: x.Debt - x.Debt.shift(1))).reset_index().Debt
  # df.groupby(['id']).
  df 
  </code></pre>
  
  <p>I need to get such DataFrame:</p>
  
  <p><a href="https://i.stack.imgur.com/QwVZC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QwVZC.png" alt="enter image description here"></a></p>
  
  <p>The start DataFrame: </p>
  
  <ul>
  <li>the first column is contract id;</li>
  <li>the second column is Date;</li>
  <li>the third column is coeficient of prepayment (EAR);</li>
  <li>4-th column is contract payment;</li>
  </ul>
  
  <p>The result DataFrame:</p>
  
  <ul>
  <li>5-th column is EAR. It equels Ear_coef(t) * Debt_with_EAR(t-1)</li>
  <li>6-th column is Debt_with_EAR. It equels Debt_with_EAR(t-1)+Contract(t)+EAR(t)</li>
  </ul>
  
  <p>Ear and Debt_with_EAR at first date equels 0 and Debt at  respectively.</p>
  
  <p>I have tried to solve such task with apply. But I have not had a success since I need to use previous value which is also calculated. 
  This answers do not help me <a href="https://stackoverflow.com/questions/34855859/is-there-a-way-in-pandas-to-use-previous-row-value-in-dataframe-apply-when-previ/54420250#54420250">Is there a way in Pandas to use previous row value in dataframe.apply when previous value is also calculated in the apply?</a>
  since I Have hundreds id.</p>
  
  <p>I will be grateful for the help.</p>