Skip to main content
 首页 » 编程设计

python pandas 嵌套循环 : to apply a function to each element of e. g。第 2 列涉及在前面的第 1 列中混合相同的元素

2024年10月24日27jirigala

我是 pandas/data frame 编程的新手,还没有掌握实现简单循环的等价物。

我基本上想实现的是这样的:

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 *端口 4 bin 1 的 rebal_weight 等)


其实我觉得我要达到的是一个累积的产物。
从上面看,

value[port[counter +1, bin]] = port[counter + 1, bin]*port[counter, bin] 

因此按理说任何值[port,bin]都可以表示为值[port-1,bin]的乘积。因此,从 value[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 到 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 

实际需要的输出:

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