将Excel公式转换为PythonPandas代码

Convert Excel Formula to Python Pandas code

您好,我有一个 excel 公式可以计算累加和求和,我尝试用 python pandas 使用 pd.cumsum() 和 [=15= 复制公式] 但我没有得到准确的输出。

excel公式是=PRODUCT(1+$B:B6)^(4/COUNT($B:B6))-1[单元格c6]

提前致谢。

数据:

Effective Date  Balanced YFYS   Annualised since incpetion
30-09-2014      0.01382434  
31-12-2014      0.03070278  
31-03-2015      0.04251546  
30-06-2015      -0.01200241     7.63%
30-09-2015      -0.00109375     5.97%
31-12-2015      0.02013989      6.35%
31-03-2016      0.0072328       5.86%
30-06-2016      0.02630933      6.48%
30-09-2016      0.02090324      6.72%
31-12-2016      0.01165302      6.52%
31-03-2017      0.02086588      6.71%
30-06-2017      0.01677891      6.72%

$ in excel 是固定索引。所以你想从 3rd 行中分割你的数据。尝试:

# 1 corresponds to column B
s = df.iloc[:,1]

df.loc[s.index, 'annulized'] = (1+s).cumprod() ** (4/s.notna().cumsum()) - 1

输出:

   Effective Date  Balanced YFYS Annualised since incpetion  annulized
0      30-09-2014       0.013824                       None   0.056455
1      31-12-2014       0.030703                       None   0.091924
2      31-03-2015       0.042515                       None   0.120912
3      30-06-2015      -0.012002                      7.63%   0.076303
4      30-09-2015      -0.001094                      5.97%   0.059662
5      31-12-2015       0.020140                      6.35%   0.063521
6      31-03-2016       0.007233                      5.86%   0.058556
7      30-06-2016       0.026309                      6.48%   0.064789
8      30-09-2016       0.020903                      6.72%   0.067155
9      31-12-2016       0.011653                      6.52%   0.065166
10     31-03-2017       0.020866                      6.71%   0.067053
11     30-06-2017       0.016779                      6.72%   0.067201