Search code examples
pythonpandasdataframefinance

How to multiply value in previous row with a value from current row


I can't seem to find a way around the following problem. I have the following dataframe:

Daily Return(%)       
   -0.1                   
    0.1                   
    0.2                   
   -.01                   
    .04                   
   0.12                   

and I want to have the portfolio value calculated. The initial value of my portfolio is 500 and after the calculation I should have the following dataframe

Daily Return(%)     Portfolio Value     
       -0.1                   450
        0.1                   495
        0.2                   594
       -.01                   588
        .04                   611
       0.12                   684

The forumla of calculating the portfolio value is 500+(500x(-0.1))+(450x0.1)+(495x0.2)....

I am struggling to put the formula above into code. Any help or guidance would be highly appreciated


Solution

  • Let us add one then cumprod

    df['Daily Return(%)'].add(1).cumprod()*500
    0    450.000000
    1    495.000000
    2    594.000000
    3    588.060000
    4    611.582400
    5    684.972288
    Name: Daily, dtype: float64
    df['p Value'] = df['Daily Return(%)'].add(1).cumprod()*500