0

My pandas data frame looks like this:

      id          obal            sprin  prepayment       obal_prepay
      1           100               10    0.02
      1           90                15    0.03
      1           75                20    0.04
        ...           ...        ...         ...
      2           150               10    0.02
      2           140               10    0.03
      2           120               20    0.04

I need to apply following group wise operations:

  1. for every id-group, I need to add 1 row at the beginning which will contain one specific number of a different column.
  2. for every id-group I have to do following "recurisve" calculation to get the "obal_prepay" values: obal_prepay[i]=obal[i-1]-prepayment*[obal_prepay[i-1]-sprin[i] For one simple example in an numpy array the code would look like this:

       for i in range(1,len(id)):
        array[i,4]=array[i-1,0]-array[i-1,4]*array[i,3]- array[i,2]
    

The final data frame should look like this

 id          obal            sprin  prepayment       obal_prepay
  1           100                 -      -            100
  1           100               10    0.02            100-100*0.02-10=88
  1           90                15    0.03            100-88*0.03-15=82.36    
  1           75                20    0.04            90-82.36*0.04-20=...
    ...           ...        ...         ...
  2           150                -    -               150
  2           150               10    0.02            150-150*0.02-10=137
  2           140               10    0.03            .. .
  2           120               20    0.04            ..

Hope that was clear enough!

Thanks,

KS

Kosta S.
  • 339
  • 1
  • 2
  • 11
  • 1
    It would greatly help if you were to create a smaller less complex set of sample data with the expected output. Take a look at https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – Scott Boston Dec 18 '18 at 14:48
  • Thanks for your remark. I adjuted my question accordingly. – Kosta S. Dec 18 '18 at 15:06
  • You may need a for loop here – BENY Dec 18 '18 at 15:07
  • maybe write an own function for-loop and call it with apply? – Kosta S. Dec 18 '18 at 15:12
  • So I'm thinking of splitting my data set into grouped lists, which then would serve as an input for my loop-function..Is there a more efficient way of doing that? – Kosta S. Dec 19 '18 at 09:11

0 Answers0