python - 如何滚动添加以前的数据百分比
问题描述
我想做一个计算,其中以前的数据是百分比添加了这样的数据框:
a = 100
以 10% 的增长增长,之前添加如下:
df = [110,121,133.10,146.41]
解决方案
Dataframe 列是pandas.Series对象,可以转换为numpy.narray
. 因此,您可以对底层数组进行操作。
要实现代表 10 % 增加/增长的用户定义的累积函数,我们可以结合numpy.frompyfunc
和numpy.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]
推荐阅读
- sql - Postgres 在加入时无法识别 CAST 的问题
- javascript - 在将 href 添加到 url 时加载新组件
- python - 如何在 sqlite3 python 中返回一个以上的字母查询?
- android - 无法将 LinearLayout 放在页面底部 android studio
- html - HTML:本地 web css 是指互联网链接?
- python - 在按 col A 和 B 分组时,在 pandas 数据框中找到 col C 的最大值
- javascript - 如何清除导致错误的搜索参数?
- node.js - 部分代码用电子打包器打包后无法使用
- mysql - 插入后显示事件在mysql中不起作用,或者没有数据保存在那里
- android - 获取android内部存储中的所有文件