pandas当前行*上一行+上一行

时间:2017-05-03 08:49:10

标签: python pandas

index_pd:

tradedate  |  percent  |  day_index
------ | ------| ------
2015-06-02 |    0      |     1000
2015-06-03 |    0.5    |     0
2015-06-04 |    0.6    |     0
.....

想要结果:

tradedate  |  percent  |  day_index
------ | ------| ------
2015-06-02 |    0      |     1000
2015-06-03 |    0.5    |     1500 = 1000 + 1000 * 0.5
2015-06-04 |    0.6    |     2400 = 1500 + 1500 * 0.6
.....

我试试

index_pd['day_index'] =index_pd['day_index'].shift(1) * index_pd['percent'].shift(0) + index_pd['day_index'].shift(1)

但它会影响第二排。 index_pd中有一千行,如何批量替换,谢谢

2 个答案:

答案 0 :(得分:1)

不是很好的解决方案,因为iterrows循环:

for i, row in index_pd.iterrows():
    if i == 0:
        index_pd.loc[i, 'value'] = index_pd['day_index'].iat[0]
    else:
        index_pd.loc[i, 'value'] = index_pd.loc[i,'percent']  * index_pd.loc[i-1, 'value']+ \
                                   index_pd.loc[i-1, 'value']
print (index_pd)
    tradedate  percent  day_index   value
0  2015-06-02      0.0       1000  1000.0
1  2015-06-03      0.5          0  1500.0
2  2015-06-04      0.6          0  2400.0

答案 1 :(得分:0)

在计算的每一行中,先前的day_index值乘以(1+index_pd.percent)。因此,您可以使用(1+index_pd.percent)的累积乘积,并将其乘以day_index的第一个值来获得结果:

index_pd['day_index'] = (index_pd.percent+1).cumprod()*index_pd.day_index.iat[0]