我正在将一个有55行的dataframe df
中的数据插入到一个形状为 (55,60) 的numpy数组 matrix_of_coupons_and_facevalues
中,使用以下代码。然而,我遇到了错误IndexError: index 55 is out of bounds for axis 0 with size 55
。变量months_to_maturity
包含数字[6:6:330]
。
for (i,row) in df.iterrows():
matrix_of_coupons_and_facevalues[i,0:(row.months_to_maturity/ 6)-1] = 1/2
matrix_of_coupons_and_facevalues[i,(row.months_to_maturity/6)-1] = 3/2
谢谢您
df
确实有55行吗?从代码的样子来看,i
似乎是取了55的值,这只有在df
有超过55行的情况下才可能发生。也许你可以尝试像这样打印一下print(len(list(df.iterrows())))
看看是否确实是55。 - brenns10print(len(list(df.iterrows())))
的结果是55。 - user131983