繁体   English   中英

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

[英]python pandas dataframe create new column from other columns' cells

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

         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

我想添加一个名为“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 

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

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

df["log_ret"] = df[df["instrument_holding"] + "_return"]

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

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

使用DF.assign将分配给新列log_ret

df.assign(log_ret=np.where(df.instrument_holding == 'a', df.a_return, df.b_return))

在此输入图像描述

  • 使用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

使用apply 这不是最神奇的方式,但它非常灵活。

def select(row):
    if row['instrument_holding'] == 'a':
        return row['a_return']
    else:
        return row['b_return']

df['log_ret'] = df.apply(select, axis=1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM