熊猫Python中的累积乘法

时间:2019-04-04 14:21:54

标签: python pandas cumulative-sum

我有一个如下数据框:

计算累积回报以填补Nan值的最佳方法是什么?显示了每个单元格的逻辑。

以下是预期的结果:

import pandas as pd
df = pd.DataFrame({"DATE":[2018,2019,2020,2021,2022,2023,2024],"RATIO":[0.03,0.04,0.05,0.06,0.07,0.08,0.09],"PROFIT":[10,20,np.nan,np.nan,np.nan,np.nan,np.nan]})


df.loc[df['DATE']==2020, ['PROFIT']] = 20000*(1+0.04)
df.loc[df['DATE']==2021, ['PROFIT']] = 20000*(1+0.04)*(1+0.050)
df.loc[df['DATE']==2022, ['PROFIT']] = 20000*(1+0.04)*(1+0.050)*(1+0.060)
df.loc[df['DATE']==2023, ['PROFIT']] = 20000*(1+0.04)*(1+0.050)*(1+0.060)*(1+0.070)
df.loc[df['DATE']==2024, ['PROFIT']] = 20000*(1+0.04)*(1+0.050)*(1+0.060)*(1+0.070)*(1+0.080)

df

1 个答案:

答案 0 :(得分:2)

您正在寻找cumprod

df['PROFIT']=df['PROFIT'].fillna(df.RATIO.shift().add(1).iloc[2:].cumprod()*20000)
df
Out[30]: 
   DATE  RATIO       PROFIT
0  2018   0.03     10.00000
1  2019   0.04     20.00000
2  2020   0.05  20800.00000
3  2021   0.06  21840.00000
4  2022   0.07  23150.40000
5  2023   0.08  24770.92800
6  2024   0.09  26752.60224