1 回答
TA贡献1805条经验 获得超9个赞
我认为需要sub带有参数axis=0的匹配索引,DataFrame该索引的索引为Series:
轴:{0,1,'索引','列'}
对于系列输入,轴与系列索引匹配
excess_ret = returns.sub(basket, axis=0)
print (excess_ret)
AAPL GOOG MSFT AMZN FB
2018-01-01 NaN NaN NaN NaN NaN
2018-01-02 -1.833226 -0.110935 0.455586 -0.173553 1.662127
2018-01-03 -0.662713 1.737714 -1.295243 1.381853 -1.161611
2018-01-04 3.269817 -0.824819 0.377973 -0.788368 -2.034604
2018-01-05 -0.082528 1.814466 2.295359 -3.543489 -0.483808
2018-01-06 0.295950 2.978380 1.000856 1.346977 -5.622164
2018-01-07 1.988864 -2.316191 0.633370 1.043901 -1.349943
2018-01-08 -2.640122 -0.861669 -1.472634 -1.559951 6.534376
2018-01-09 8.062484 -1.712583 -2.497513 -0.807566 -3.044822
2018-01-10 -1.823915 0.370618 -0.883559 0.888679 1.448177
如果要按列匹配:
a = returns.mean(axis=0)
print (a)
AAPL 0.088224
GOOG -1.301244
MSFT -2.436290
AMZN -1.009339
FB -0.102484
dtype: float64
excess_ret = returns.sub(a, axis=1)
print (excess_ret)
AAPL GOOG MSFT AMZN FB
2018-01-01 NaN NaN NaN NaN NaN
2018-01-02 -1.353102 1.441870 5.759181 0.421661 -0.608508
2018-01-03 -0.434575 -0.969659 0.665239 0.823154 4.917633
2018-01-04 8.771575 -2.722012 0.409977 -2.113780 -1.164615
2018-01-05 -0.220083 0.213942 1.329937 -0.372537 0.037217
2018-01-06 -0.633686 6.371478 -14.157027 -0.831583 1.226992
2018-01-07 -2.363521 0.130848 1.743317 -1.381718 -1.929583
2018-01-08 -3.062185 -6.431137 0.438800 0.956752 -1.641623
2018-01-09 -0.450300 2.093572 2.965726 -0.617335 1.042234
2018-01-10 -0.254123 -0.128903 0.844849 3.115386 -1.879747
添加回答
举报