python pandas dataframe从其他列的单元格创建新列

jos*_*hwa 7 python dataframe pandas

我有这样的数据框......

         a_return    b_return  bc_ratio instrument_holding 
0             NaN         NaN -0.165286                  a 
1        0.996474    1.013166 -0.164637                  a   
2        0.997730    0.993540 -0.170058                  a   
3        1.024294    1.024318 -0.184530                  a   
4        1.019071    1.047297 -0.148644                  a   
5        0.992243    1.008210 -0.188752                  a    
6        1.010331    1.039020 -0.098413                  a   
7        0.989542    0.991899  0.025051                  b   
8        1.005197    1.002527 -0.025051                  b 
9        0.990755    1.002352 -0.099800                  a  
10       1.006241    0.998375 -0.078643                  b
Run Code Online (Sandbox Code Playgroud)

我想添加一个名为"log_ret"的列,其中"a_return"或"b_return"中的值将根据"instrument_holding"列中的值使用.像这样...

         a_return    b_return  bc_ratio instrument_holding   log_ret  
0             NaN         NaN -0.165286                  a       NaN  
1        0.996474    1.013166 -0.164637                  a  0.996474  
2        0.997730    0.993540 -0.170058                  a  0.997730  
3        1.024294    1.024318 -0.184530                  a  1.024294  
4        1.019071    1.047297 -0.148644                  a  1.019071  
5        0.992243    1.008210 -0.188752                  a  0.992243  
6        1.010331    1.039020 -0.098413                  a  1.010331  
7        0.989542    0.991899  0.025051                  b  0.991899  
8        1.005197    1.002527 -0.025051                  b  1.002527  
9        0.990755    1.002352 -0.099800                  a  0.990755  
10       1.006241    0.998375 -0.078643                  b  0.998375 
Run Code Online (Sandbox Code Playgroud)

如您所见,如果'instrument_holding'的行值为'a','log_ret'的值为'a_return',如果'instrument_holding'的值为'b','log_ret'的值为'b_return' .

我以为它可以像这样完成......

df["log_ret"] = df[df["instrument_holding"] + "_return"]
Run Code Online (Sandbox Code Playgroud)

事实并非如此.谢谢你的任何建议!

Nic*_*eli 8

一种可能性是np.whereinstrument_holding等于的条件下使用,如果条件满足则"a"返回a_return列中的相应值,否则返回另一列.

用于稍后DF.assign将分配到新列log_ret.

df.assign(log_ret=np.where(df.instrument_holding == 'a', df.a_return, df.b_return))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


piR*_*red 7

  • 使用map更改值instrument_holding
  • 使用 lookup

df.assign(
    log_return=df.lookup(df.index, df.instrument_holding.map('{}_return'.format)))

    a_return  b_return  bc_ratio instrument_holding  log_return
0        NaN       NaN -0.165286                  a         NaN
1   0.996474  1.013166 -0.164637                  a    0.996474
2   0.997730  0.993540 -0.170058                  a    0.997730
3   1.024294  1.024318 -0.184530                  a    1.024294
4   1.019071  1.047297 -0.148644                  a    1.019071
5   0.992243  1.008210 -0.188752                  a    0.992243
6   1.010331  1.039020 -0.098413                  a    1.010331
7   0.989542  0.991899  0.025051                  b    0.991899
8   1.005197  1.002527 -0.025051                  b    1.002527
9   0.990755  1.002352 -0.099800                  a    0.990755
10  1.006241  0.998375 -0.078643                  b    0.998375
Run Code Online (Sandbox Code Playgroud)