[英]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:我有以下 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我的投资组合的初始值为 500,计算后我应该有以下 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)....计算投资组合价值的公式是 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任何帮助或指导将不胜感激
Let us add
one then cumprod
让我们add
一个然后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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.