简体   繁体   English

Pandas dataframe cumprod by columns

[英]Pandas dataframe cumprod by columns

I have a daframe with yearly performance of three items.我有一个 daframe,每年有三个项目的表现。 I want to add another column with the cumulated product by columns.我想添加另一列,其中包含按列累积的产品。 The cumulated product of x is 0,42%. x 的累积乘积为 0.42%。 I tried with pandas cumprod either with axis=0 and 1 but it give me a error.我尝试使用 pandas cumprod 或者 axis=0 和 1 但它给了我一个错误。 There is a way to do this?有办法做到这一点吗?

2016  2017  2018 
2,00% -3,00% 1,50%
4,00%  2,00% -1,00%
-5,00%  7,00% -2,50%

2016  2017  2018  TOTAL CUMULATED
2,00% -3,00% 1,50% 0,42%
4,00%  2,00% -1,00% 5,02%
-5,00%  7,00% -2,50% -0,89%

This is my code but it's not correct:这是我的代码,但它不正确:

import pandas as pd
import numpy as np
data = [[0.02, -0.03, 1.50], [0.04, 0.02, -1.00], [-0.05, 0.07, -2.50]]
df = pd.DataFrame(data, columns=['2016', '2017', '2018'])
df['TOTAL CUMULATED'] = np.cumprod(1 + df) - 1

It give me:ValueError: Wrong number of items passed 3, placement implies 1它给了我:ValueError:错误的项目数通过 3,位置意味着 1

So, the code is not correct because it add a row at the end of the dataframe, instead i want to add another column with the cumprod of rows因此,代码不正确,因为它在 dataframe 的末尾添加了一行,而不是我想添加另一列与行的 cumprod

Here it seems you want pandas.prod not np.cumprod .在这里,您似乎想要pandas.prod而不是np.cumprod This should work:这应该有效:

df['TOTAL CUMULATED'] = (df+1).prod(axis=1) - 1

produces生产

      2016    2017    2018    TOTAL CUMULATED
--  ------  ------  ------  -----------------
 0    0.02   -0.03   0.015          0.004241
 1    0.04    0.02  -0.01           0.050192
 2   -0.05    0.07  -0.025         -0.0089125

Note I had to fix your df construction as the last column was off by x100注意我必须修复您的df构造,因为最后一列已关闭 x100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM