python - Inserting data from dataframe into numpy array -


i inserting data dataframe df 55 rows numpy array matrix_of_coupons_and_facevalues shape of (55,60). doing using code below. however, error indexerror: index 55 out of bounds axis 0 size 55. months_to_maturity contains numbers [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 

thank you

for future visitors, here's happened:

a dataframe's index serves label each row uniquely, when delete row, index removed , have "gap" in index. when have meaningful index. but, when want index number rows, it's not want. in case, df contained 55 rows, index had holes, largest index larger 55, causing indexerror in matrix. example:

in [1]: import pandas pd  in [2]: df = pd.dataframe([[1,2],[3,4],[5,6]], columns=['x','y'])  in [3]: df out[3]:     x  y 0  1  2 1  3  4 2  5  6  in [4]: df = df.drop(1)  in [5]: df out[5]:     x  y 0  1  2 2  5  6 

in order remedy situation, can reassign index list containing correct range of numbers:

in [6]: df.index = list(range(len(df.index)))  in [7]: df out[7]:     x  y 0  1  2 1  5  6 

Comments