[英]python pandas nested loop: to apply a function to each element of e.g. column 2 involving compounding same elements in previous column 1s
I am new to pandas/data frame programming, and have not yet grasped implementing the equivalent of simple loops.我是熊猫/数据框编程的新手,还没有掌握实现简单循环的等价物。
What I am essentially trying to implement is something like this:我基本上想要实现的是这样的:
counter = 0
for port in portfolios:
for bin in bins:
value[port[counter +1, bin]]
= port[counter + 1, bin] * port[counter, bin]
counter += 1
In fact, using an example table below, for each bin in each portfolio port, I am trying to multiply rebal_wgt [port=i,bin=j]
by rebal_wgt[port=i-1,bin=j]
.事实上,使用下面的示例表,对于每个投资组合端口中的每个 bin,我试图将rebal_wgt [port=i,bin=j]
乘以rebal_wgt[port=i-1,bin=j]
。 I want to do this for all portfolios port (there are 11 of them), and all the bins within each portfolio port, such that each portfolio's corresponding bin gets multiplied by the previous portfolio's corresponding bin (eg port5 bin 1 rebal_weight * port 4 bin 1's rebal_weight
etc.)我想对所有投资组合端口(其中有 11 个)以及每个投资组合端口内的所有 bin 执行此操作,以便每个投资组合对应的 bin 乘以前一个投资组合的对应 bin(例如port5 bin 1 rebal_weight * port 4 bin 1's rebal_weight
等)
In fact, I think that what I am trying to achieve is a cumulative product.事实上,我认为我试图实现的是一个累积的产品。
From above,从上面,
value[port[counter +1, bin]] = port[counter + 1, bin]*port[counter, bin]
So it stands to reason that any value[port,bin] can be expressed as a product with the value[port-1,bin].因此,任何 value[port,bin] 都可以表示为 value[port-1,bin] 的乘积。 Thus, starting with value[0,bin], one can do cumulative product to derive the values of successive portfolios for each bin.因此,从 value[0,bin] 开始,可以进行累积乘积以得出每个 bin 的连续投资组合的值。 Which is what I would like to do!这就是我想做的! unfortunately I don't think the first answer given below does this as it doesn't loop over portfolios for each bin, it seems to loop over bins within each portfolio.不幸的是,我不认为下面给出的第一个答案是这样做的,因为它没有循环每个 bin 的投资组合,它似乎循环每个投资组合中的 bin。 Any help much appreciated!非常感谢任何帮助! a sample data table is below, ignore cum_ret_sum and stocks.下面是一个示例数据表,忽略 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
Example output for bin = 0 (want this for all bin = 0 to bins) bin = 0 的示例输出(希望所有 bin = 0 到 bins)
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
Actual desired output:实际所需的输出:
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
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.