我正在尝试把一个滚动累积的产品系列在熊猫。我的输入序列是:
s
0 1
1 2
2 3
3 4
4 5
5 6
我想得到一个结果序列,它给出了前面n个值的累积积。如果n是3,我想得到:
^{pr2}$我想出的代码使用rolling_apply和lambda函数,并生成一个TypeError:
import pandas as pnd
df = pnd.DataFrame()
df['s'] = [1,2,3,4]
print (df)
print (pnd.rolling_apply(df.s,2,lambda x : x.cumprod()))
TypeError: only length-1 arrays can be converted to Python scalars
有人知道怎么做吗?在
感谢用户57123。这是我想出的代码,它能起作用。。。在
相关问题 更多 >
编程相关推荐