想象一下,我有一个包含多重指数(股票和日期)的数据框。我想从关闭列计算pct_change。我使用以下代码:
data['win'] = data.Close.pct_change()*100
Open Close High Low Volume Adj_Close win
Ticker Date
AAPL 2018-12-14 169.000000 165.479996 169.080002 165.279999 40703700 165.479996 NaN
2018-12-17 165.449997 163.940002 168.350006 162.729996 44287900 163.940002 -0.930622
2018-12-18 165.380005 166.070007 167.529999 164.389999 33841500 166.070007 1.299259
2018-12-19 166.000000 160.889999 167.449997 159.089996 48889400 160.889999 -3.119171
AMZN 2018-12-14 1638.000000 1591.910034 1642.569946 1585.000000 6367200 1591.910034 **889.440017**
2018-12-17 1566.000000 1520.910034 1576.130005 1505.010010 8829800 1520.910034 -4.460051
2018-12-18 1540.000000 1551.479980 1567.550049 1523.010010 6523000 1551.479980 2.009977
2018-12-19 1543.050049 1495.079956 1584.530029 1483.180054 8654400 1495.079956 -3.635240
它工作正常,但是当它以AMZN pct_change开头时,第一个错误,因为它使用了AAPL的最后一个值。
如何更改公式以正确计算pct_change?
解决方案应该是这样:
Open Close High Low Volume Adj_Close win
Ticker Date
AAPL 2018-12-14 169.000000 165.479996 169.080002 165.279999 40703700 165.479996 NaN
2018-12-17 165.449997 163.940002 168.350006 162.729996 44287900 163.940002 -0.930622
2018-12-18 165.380005 166.070007 167.529999 164.389999 33841500 166.070007 1.299259
2018-12-19 166.000000 160.889999 167.449997 159.089996 48889400 160.889999 -3.119171
AMZN 2018-12-14 1638.000000 1591.910034 1642.569946 1585.000000 6367200 1591.910034 Nan
2018-12-17 1566.000000 1520.910034 1576.130005 1505.010010 8829800 1520.910034 -4.460051
2018-12-18 1540.000000 1551.479980 1567.550049 1523.010010 6523000 1551.479980 2.009977
2018-12-19 1543.050049 1495.079956 1584.530029 1483.180054 8654400 1495.079956 -3.635240