I am trying to insert data from a dataframe df into a numpy array matrix_of_coupons_and_facevalues. However, I basically need to add the value associated with a row of df['Coupon'] into each column of a corresponding row of the array for as many columns as the number numberofcoupon_payments = row1['months_to_maturity']/6. I get the error ValueError: could not broadcast input array from shape (1,2) into shape (1,61) in the line np.insert(matrix_of_coupons_and_facevalues, row_no+1, rowtobeadded, 0) and I understand why, but I don't know how to proceed.
The code I am using is as 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)
for (i1,row1) in df.iterrows():
numberofcoupon_payments = row1['months_to_maturity']/6
for row_no in range(int(number_of_rows_and_columns)):
for 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:
The dataframe df looks like 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:
For example I need the array to look like this, if the 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