python - ValueError when inserting data into numpy Array -


i trying insert data dataframe df numpy array matrix_of_coupons_and_facevalues. however, need add value associated row of df['coupon'] each column of corresponding row of array many columns number numberofcoupon_payments = row1['months_to_maturity']/6. error valueerror: not broadcast input array shape (1,2) shape (1,61) in line np.insert(matrix_of_coupons_and_facevalues, row_no+1, rowtobeadded, 0) , understand why, don't know how proceed.

the code using follows:

matrix_of_coupons_and_facevalues = np.zeros((number_of_rows_and_columns, number_of_rows_and_columns))         rowtobeadded = np.zeros(number_of_rows_and_columns)         (i1,row1) in df.iterrows():             numberofcoupon_payments = row1['months_to_maturity']/6             row_no in range(int(number_of_rows_and_columns)):                 index_no in range(int(numberofcoupon_payments)):                     coupon = row1['coupon']                     rowtobeadded = np.full((1, numberofcoupon_payments), coupon)                     np.insert(matrix_of_coupons_and_facevalues, row_no+1, rowtobeadded, 0) 

edit: dataframe df looks this:

   months_to_maturity          on_the_run_dt          asset_id  \ 0                    5  2015-07-02 00:00:00.0  00102cc07f4b02ca    1                    6  2015-06-25 00:00:00.0  00102cd0fb2a023f    2                   11  2015-04-02 00:00:00.0  00102cfed3c500d4    3                   12  2015-06-25 00:00:00.0  00102c37122b0230    4                   23  2015-03-02 00:00:00.0  00102c76082b0069                orig_iss_dt            maturity_dt  pay_freq_cd  coupon  \ 0   2015-07-02 00:00:00.0  2015-12-31 00:00:00.0          nan   0.000    1   2015-06-25 00:00:00.0  2015-12-24 00:00:00.0          nan   0.000    2   2015-04-02 00:00:00.0  2016-03-31 00:00:00.0          nan   0.000    3   2015-06-25 00:00:00.0  2016-06-23 00:00:00.0          nan   0.000    4   2015-03-02 00:00:00.0  2017-02-28 00:00:00.0            2   0.500          closing_price cpn_type_cd  months_to_maturity_1  face_value   0       99.944389        fxdi                     5  24000101.6   1       99.960889        fxdi                     6  24000366.4   2       99.866806        fxdi                    11  25000267.5 

desired output array: example need array this, if months_to_maturity column of df has values 6,12,18:

array([[coupon     0     0      0],         [coupon   coupon  0      0],         [coupon   coupon coupon  0]]) 

thank you

this should trick:

df = pd.dataframe({'months_to_maturity':[5,6,11,12,23],'coupon' : [0,0,0,0,.5]}) matrix_of_coupons_and_facevalues = np.zeros((5,5))  i,row in df.iterrows():     matrix_of_coupons_and_facevalues[i,0:row.months_to_maturity/6] = row['coupon']      matrix_of_coupons_and_facevalues array([[ 0. ,  0. ,  0. ,  0. ,  0. ],        [ 0. ,  0. ,  0. ,  0. ,  0. ],        [ 0. ,  0. ,  0. ,  0. ,  0. ],        [ 0. ,  0. ,  0. ,  0. ,  0. ],        [ 0.5,  0.5,  0.5,  0. ,  0. ]]) 

Comments

Popular posts from this blog

round image using picasso in android -

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

python - How to remove the Xframe Options header in django? -