简体   繁体   English

如何将前一行中的值与当前行中的值相乘

[英]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.

相关问题 Python Pandas:有没有办法使用前一行值并将其乘以每一行的当前行 - Python Pandas: Is there a way to use the previous row value and multiply it by the current row for every row 从Pandas列中的当前行值中减去前一行的值 - Subtract previous row value from the current row value in a Pandas column 如何根据 pandas DataFrame 中的条件从当前行值中减去前一行值? - how to subtract previous row value from current row value based on condition in pandas DataFrame? 根据条件从当前行和上一行中的值创建列表 - Create list from value in current row and previous row based on condition 将当前行值与前一行值进行比较 - Compare current row value to previous row values 如何将当前行与前一行进行比较并增加 Mode 值 - How to compare current row with the previous row and increment the Mode value 如何将行值与前一行值进行比较? - How to compare row value with previous row value? 如果列等于 Pandas 中的值,如何使当前行值等于前一行值? - How to make current row value equal to previous row value if a column equals a value in Pandas? 如何获取 Dataframe 中上一行的值以更改当前行值(动态地,而不是静态值) - How can I get the value of the previous row in a Dataframe to change the current row value (dynamically, not with a static value) 如何将 dataframe 中的每一行乘以 csv 文件中的值 - How to multiply every row in dataframe by value from csv file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM