我正在尝试使用函数pandas.ewm.cov()计算一组数据之间的指数加权协方差。 听起来很傻,但我无法访问结果:特别是如何获取最后一个矩阵(在本例中为第999个)作为pandas数据帧(切片等似乎也影响内部索引0,1,2)。 有没有人能提供高效,可扩展的解决方案?
import pandas as pd
import numpy as np
if __name__ == "__main__":
df = pd.DataFrame(np.random.randn(1000, 3))
covs = df.ewm(span = 180).cov()
print(covs)
print(covs.iloc[-3:,:]) #this does not work anymore If I change the
# dimension of the df from 3 i.e. to 4.
0 1 2
0 0 NaN NaN NaN
1 NaN NaN NaN
2 NaN NaN NaN
1 0 0.032264 -0.163599 -0.125954
1 -0.163599 0.829545 0.638665
2 -0.125954 0.638665 0.491707
... ... ... ...
999 0 0.878615 0.011006 0.157368
1 0.011006 0.905043 0.078750
2 0.157368 0.078750 1.058194
-----------------------------------
0 1 2
999 0 1.160964 0.03044