【问题标题】:How to Rolling Add The Previous Data With Percentage如何滚动添加以前的数据百分比
【发布时间】:2021-04-12 12:23:58
【问题描述】:

我想做一个计算,其中以前的数据是百分比相加的 像这样的数据框:

a = 100

增长 10%,之前添加的增长是这样的:

df = [110,121,133.10,146.41]

【问题讨论】:

  • 在您的示例数据中不应使用133.10 代替133
  • @大威没错,是真的,
  • 问一个问题时请更具体一点:到目前为止,您尝试了哪些代码示例?你能指望什么?你得到什么错误?如需帮助,请查看“How to ask”。
  • @albert 我已经在 pandas 中使用循环尝试过,但它不起作用,这只是一个简单的指数增长,

标签: python pandas dataframe


【解决方案1】:

数据框列是pandas.Series 对象,其中can be converted intonumpy.narray。因此,您可以对底层数组进行操作。

要实现代表 10 % 增长/增长的用户定义累积函数,我们可以结合 numpy.frompyfuncnumpy.ufunc.accumulate

import numpy as np

arr = [100, 100, 100, 100, 100]

uadd = np.frompyfunc(lambda x, y: x*1.1, 2, 1)
out = uadd.accumulate(arr, dtype=object).astype(float)[1:]

print(out)

印刷:

[110.   121.   133.1  146.41]

this answeranother question 中描述了类似的方法,我想将其作为进一步参考。

【讨论】:

    猜你喜欢
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    相关资源
    最近更新 更多