我正在尝试将数据框df
中的数据插入到numpy数组matrix_of_coupons_and_facevalues
中。但是,我基本上需要将与df['Coupon']
行关联的值添加到数组的相应行的每列中,以获得与数字numberofcoupon_payments = row1['months_to_maturity']/6
一样多的列。我在行ValueError: could not broadcast input array from shape (1,2) into shape (1,61)
中收到错误np.insert(matrix_of_coupons_and_facevalues, row_no+1, rowtobeadded, 0)
,我理解为什么,但我不知道如何继续。
我使用的代码如下:
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)
修改:
数据框df
如下所示:
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
所需的输出数组:
例如,如果months_to_maturity
的{{1}}列的值为df
,6
,12
,我需要数组看起来像这样:
18
谢谢
答案 0 :(得分:2)
这应该可以解决问题:
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))
for 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. ]])