title: How to populate data frame based on previous row values with pandas?
body: <p>I have the following dataframe:</p>
  <pre class="lang-py prettyprint-override"><code>df = pd.DataFrame(data={
      &quot;id&quot;: ['a', 'd'],
      &quot;amount&quot;: [3000, 4000],
      &quot;rate&quot;: [0.2, 0.3],
      &quot;date&quot;: [&quot;2022-07-21&quot;, &quot;2022-08-11&quot;],
      &quot;months&quot;: [4, 5],
  })
  </code></pre>
  <p>I want to create installments for the given trades. Output should look like this:</p>
  <pre><code>  id    interest   principal        date       balance  installment
  0  a   50.000000  731.508250  2022-07-21  2.268492e+03   781.508250
  1  a   37.808196  743.700055  2022-08-21  1.524792e+03   781.508250
  2  a   25.413195  756.095055  2022-09-21  7.686966e+02   781.508250
  3  a   12.811611  768.696640  2022-10-21  0.00000        781.508250
  4  d  100.000000  760.987444  2022-08-11  3.239013e+03   860.987444
  5  d   80.975314  780.012130  2022-09-11  2.459000e+03   860.987444
  6  d   61.475011  799.512433  2022-10-11  1.659488e+03   860.987444
  7  d   41.487200  819.500244  2022-11-11  8.399877e+02   860.987444
  8  d   20.999694  839.987750  2022-12-11  0.00000        860.987444
  </code></pre>
  <p>Key point here is that the next row values depend on previous <code>balance</code> value. The first time <code>balance</code> is <code>amount</code> of the source data frame.</p>
  <p>My current solution:</p>
  <pre class="lang-py prettyprint-override"><code>import numpy_financial as npf
  import pandas as pd
  from dateutil.relativedelta import relativedelta
  
  df = pd.DataFrame(data={
      &quot;id&quot;: ['a', 'd'],
      &quot;amount&quot;: [3000, 4000],
      &quot;rate&quot;: [0.2, 0.3],
      &quot;date&quot;: [&quot;2022-07-21&quot;, &quot;2022-08-11&quot;],
      &quot;months&quot;: [4, 5],
  })
  
  
  def get_output_df(df):
      columns = [&quot;id&quot;, &quot;interest&quot;, &quot;principal&quot;, &quot;date&quot;, &quot;balance&quot;]
      output_df = pd.DataFrame(columns=columns)
  
      for _, row in df.iterrows():
          rate = row[&quot;rate&quot;] / 12
          months = row[&quot;months&quot;]
          amount = row[&quot;amount&quot;]
          date = pd.to_datetime(row[&quot;date&quot;]).date()
          installment_amount = npf.pmt(rate=rate, nper=months, pv=-amount)
          prior_balance = amount
  
          loan_installment_data = []
          for i in range(months):
              interest_amount = rate * prior_balance
              principal_amount = installment_amount - interest_amount
              balance = prior_balance - principal_amount
  
              loan_installment_data.append(
                  {
                      &quot;id&quot;: row[&quot;id&quot;],
                      &quot;interest&quot;: interest_amount,
                      &quot;principal&quot;: principal_amount,
                      &quot;date&quot;: date,
                      &quot;installment&quot;: installment_amount,
                      &quot;balance&quot;: balance
                  }
              )
  
              prior_balance = balance
              date += relativedelta(months=1)
  
          output_df = output_df.append(loan_installment_data, ignore_index=True)
      return output_df
  
  
  output_df = get_output_df(df)
  </code></pre>
  <p>Is there any pandas feature i could use to do the same implementation?</p>