[英]Create a new dataframe column as a product of all the rows of another column
I have the below dataframe:我有以下 dataframe:
a
0 1
1 2
2 3
3 4
I am looking to create a new column 'b' such as the value of the row i of 'b' is defined as the product of all the previous rows of 'a', minus 1:我正在寻找创建一个新列'b',例如'b'的第i行的值被定义为'a'的所有先前行的乘积,减1:
row_b(i) = row_a(i)*row_a(i-1)*row_a(i-2)*...*row_a(0) - 1
As a result:因此:
In such a way that the final dataframe looks like:这样最终的 dataframe 看起来像:
a b
0 1 0
1 2 1
2 3 5
3 4 23
I am looking for the most pythonic and less computationally intensive way to perform this operations.我正在寻找最 Pythonic 且计算量较少的方式来执行此操作。
Thank you for your help Z谢谢你的帮助Z
As mentioned by @Quang Hong, the answer is simply正如@Quang Hong所提到的,答案很简单
df['a'].cumprod()-1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.