python - 如何在python pandas中执行累积除法
问题描述
我知道stackoverflow上的累积除法会有更多问题。但我的问题与这些问题不同。
实际上我想做计算除法,就像我们在 python 中使用计算总和(cumsum)和计算乘积(cumprod)一样。但是python中没有累积除法功能。如果您对此有任何选择,请告诉我。
问题:- 下面是我需要在其中创建折扣列并在(折扣)列中进行累积除法的数据框(DF)。
东风:-
Rates Discount
0.000028745455 1/(1+Rates)
0.000028745455 Discount[0]/(1+Rates)
0.000028745455 Discount[1]/(1+Rates)
0.000028745455 Discount[2]/(1+Rates)
0.000028745455 Discount[3]/(1+Rates)
0.000028745455 Discount[4]/(1+Rates)
0.000028745455 Discount[5]/(1+Rates)
结果:-
Rates Discount
0.000028745455 0.999971255
0.000028745455 0.999942512
0.000028745455 0.999913769
0.000028745455 0.999885026
0.000028745455 0.999856285
0.000028745455 0.999827545
0.000028745455 0.999798805
解决方案
尝试cumprod
df['out'] = (1/(df.Rates + 1)).cumprod()
Out[538]:
0 0.999971
1 0.999943
2 0.999914
3 0.999885
4 0.999856
5 0.999828
6 0.999799
Name: Rates, dtype: float64
折扣[1] = 折扣[0]/(1+费率) = 1 / (1+费率)^2
推荐阅读
- arrays - SwiftUI:在函数之外读取数组值
- drupal-7 - 有没有办法查看网站的哪些视图/阻止部分
- aws-lambda - Lambda 在 S3 中的两个文件夹之间复制 csv 文件超过 15 分钟
- jenkins - 如何使用 Jenkins 管道中的循环连接到服务器列表?
- c# - 使用 c# & ASP.Net 从 firebase 检索数据
- apache-spark - Not able to read from GreenPlum using Spark Connector
- python - 我发现了一些奇怪的东西
- java - 如何从映射文件中读取 ASCII 字符并将它们存储到字符串中?
- symfony - 数据转换器与约束
- r - 如何跨第三级命名列表 rbind data.frames?