Better way to recalculate pandas dataframe fields depending on other fields

Viewed 1599

I am new to python pandas. And i wanted to estimate a value of inflow payment/s over term considering fees and growth over a period. I only used one payment (inflow) to test. Sometimes fee2 can be applied up to period n-t. i.e. Not for the whole period, n.

I did it like below, just wondering if there is a better way to recalculate the values without looping?

Example in spreadsheet: enter image description here

Python code:

import pandas as pd
import numpy as np

def getCashFlows():
   term = 2
   growthRate = (1+0.06)**(1/12) - 1
   df = pd.DataFrame(list(range(1,term*12+1)), columns=['t'])
   df['Value_t_1'] = 0
   df['Inflow1']=0
   df['growth']=0
   df['ValuePlusGrowth'] = 0
   df['fee1']=0
   df['fee2']=30
   df['Value_t']=0

   df.set_value(0, 'Inflow1', 10000)

   for i in range(0,term*12):
      df['Value_t_1'] = df['Value_t'].shift()
      df['Value_t_1'].fillna(0,inplace=True)

      df['growth'] = (df['Value_t_1'] + df['Inflow1'])*growthRate
      df['ValuePlusGrowth'] = df['Value_t_1']+df['Inflow1']+df['growth']
      df['fee1']=df['ValuePlusGrowth']*0.5/100
      df['Value_t'] = df['ValuePlusGrowth'] - df['fee1'] - df['fee2']
   return df
1 Answers
Related