我是pandas /数据帧编程的新手,还没有掌握实现简单循环的等价物。
我本来想要实现的是这样的:
counter = 0
for port in portfolios:
for bin in bins:
value[port[counter +1, bin]]
= port[counter + 1, bin] * port[counter, bin]
counter += 1
事实上,使用下面的示例表,对于每个投资组合端口中的每个bin,我试图将rebal_wgt [port=i,bin=j]
乘以rebal_wgt[port=i-1,bin=j]
。我想对所有投资组合端口(其中有11个)以及每个投资组合端口中的所有仓位执行此操作,以使每个投资组合的相应仓位乘以先前投资组合的相应仓位(例如port5 bin 1 rebal_weight * port 4 bin 1's rebal_weight
等。)
事实上,我认为我想要实现的是累积产品 从上面,
value[port[counter +1, bin]] = port[counter + 1, bin]*port[counter, bin]
因此,任何值[port,bin]都可以表示为具有值[port-1,bin]的产品。因此,从值[0,bin]开始,可以执行累积乘积以导出每个箱的连续投资组合的值。这就是我想做的!不幸的是,我不认为下面给出的第一个答案是这样做的,因为它没有为每个bin组合循环,它似乎在每个组合中循环。任何帮助非常感谢!下面是一个样本数据表,忽略cum_ret_sum和股票。
port bin cum_ret_sum stocks rebal_wgt
0 0 0 4.067563e+03 216 1.883131e+01
1 0 1 1.300282e+04 213 6.104612e+01
2 0 2 1.426061e+04 214 6.663837e+01
3 0 3 4.904957e+02 205 2.392662e+00
4 0 4 1.100993e+04 209 5.267908e+01
5 0 5 4.630904e+03 208 2.226396e+01
6 0 6 1.019425e+04 215 4.741514e+01
7 0 7 2.249585e+04 213 1.056143e+02
8 0 8 8.831653e+03 214 4.126941e+01
9 0 9 3.098015e+05 212 1.461328e+03
10 1 0 1.881155e+00 267 7.045525e-03
11 1 1 7.486650e+00 280 2.673804e-02
12 1 2 4.492010e+00 268 1.676123e-02
13 1 3 1.191500e+01 273 4.364468e-02
14 1 4 4.388776e+00 266 1.649916e-02
15 1 5 1.384601e+01 270 5.128153e-02
bin = 0的示例输出(想要所有bin = 0到bin)
port bin rebal_wgt rebal_wgt(port[counter,bin] * port[counter - 1, bin])
0 0 18.83131173675383 18.83131173675383
0 1 61.04612379316069
0 2 66.63837376523843
0 3 2.3926618103462602
0 4 52.67907609709447
0 5 22.26396127204363
0 6 47.415137683181634
0 7 105.61432751079496
0 8 41.26940808193825
0 9 1461.3276714958988
1 0 0.0070455247063908755 0.13267647209504757
1 1 0.026738035817712412
1 2 0.01676122975460385
1 3 0.04364467915381678
1 4 0.016499159440430355
1 5 0.051281526270788164
1 6 0.04977016623588389
1 7 0.0645014820724396
1 8 0.1438106018214078
1 9 0.0340451076286303
2 0 5.1196753262692285 0.6792604605614628
2 1 0.014870173557215314
2 2 3.263374203937453
2 3 73.32640040253595
2 4 3.915173886409575
2 5 67.46028895344207
2 6 7.654613865824991
2 7 12.837204120226547
2 8 2983.065107673766
2 9 0.4204701203425892
3 0 71.16936245719319 48.34253392053873
实际所需输出:
port bin rebal_wgt rebal_wgt(port[counter,bin] * port[counter - 1, bin])
0 0 18.83131173675383 18.83131173675383
0 1 61.04612379316069 61.04612379316069
0 2 66.63837376523843 66.63837376523843
0 3 2.3926618103462602 2.3926618103462602
0 4 52.67907609709447 52.67907609709447
0 5 22.26396127204363 22.26396127204363
0 6 47.415137683181634 47.415137683181634
0 7 105.61432751079496 105.61432751079496
0 8 41.26940808193825 41.26940808193825
0 9 1461.3276714958988 1461.3276714958988
1 0 0.0070455247063908755 0.13267647209504757
1 1 0.026738035817712412 1.6322534445140364
1 2 0.01676122975460385 1.116941093152327
1 3 0.04364467915381678 0.10442695703615294
1 4 0.016499159440430355 0.8691604757005253
1 5 0.051281526270788164 1.1417299148641158
1 6 0.04977016623588389 2.3598592845892727
1 7 0.0645014820724396 6.812280652530306
1 8 0.1438106018214078 5.934978413076811
1 9 0.0340451076286303 49.75105785677358
2 0 5.1196753262692285 0.6792604605614628
2 1 0.014870173557215314 0.024271892009286238
2 2 3.263374203937453 3.6449967507110035
2 3 73.32640040253595 7.65725286445137
2 4 3.915173886409575 3.4029143975620206
2 5 67.46028895344207 77.02142996352207
2 6 7.654613865824991 18.06381160121289
2 7 12.837204120226547 87.45063726080163
2 8 2983.065107673766 17704.427018846454
2 9 0.4204701203425892 20.918833284208702
答案 0 :(得分:2)
In [5]:
df.groupby(df.bin)['rebal_wgt'].cumprod()
Out[5]:
0 18.831310
1 61.046120
2 66.638370
3 2.392662
4 52.679080
5 22.263960
6 47.415140
7 105.614300
8 41.269410
9 1461.328000
10 0.132676
11 1.632254
12 1.116941
13 0.104427
14 0.869161
15 1.141730
dtype: float64