![](/img/trans.png)
[英]pandas: Create new column by comparing DataFrame rows with columns of another DataFrame
[英]Create a new dataframe column as a product of all the rows of another column
我有以下 dataframe:
a
0 1
1 2
2 3
3 4
我正在寻找创建一个新列'b',例如'b'的第i行的值被定义为'a'的所有先前行的乘积,减1:
row_b(i) = row_a(i)*row_a(i-1)*row_a(i-2)*...*row_a(0) - 1
因此:
这样最终的 dataframe 看起来像:
a b
0 1 0
1 2 1
2 3 5
3 4 23
我正在寻找最 Pythonic 且计算量较少的方式来执行此操作。
谢谢你的帮助Z
正如@Quang Hong所提到的,答案很简单
df['a'].cumprod()-1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.