首页 > 解决方案 > 如何滚动添加以前的数据百分比

问题描述

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

a = 100

以 10% 的增长增长,之前添加如下:

df = [110,121,133.10,146.41]

标签: pythonpandasdataframe

解决方案


Dataframe 列是pandas.Series对象,可以转换numpy.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]

这个答案另一个问题中描述了类似的方法,我想提一下作为进一步的参考。


推荐阅读