Using lambda if condition on different columns in Pandas dataframe

前端 未结 2 706
情书的邮戳
情书的邮戳 2021-02-13 13:55

I have simple dataframe:

import pandas as pd
frame = pd.DataFrame(np.random.randn(4, 3), columns=list(\'abc\'))

Thus for example:



        
相关标签:
2条回答
  • 2021-02-13 14:34

    Solution

    use a vectorized approach

    frame['d'] = frame.b + (frame.c > 0) * (frame.c - frame.b)
    

    Explanation

    This is derived from the sum of

    (frame.c > 0) * frame.c  # frame.c if positive
    

    Plus

    (frame.c <= 0) * frame.b  # frame.b if c is not positive
    

    However

    (frame.c <=0 )
    

    is equivalent to

    (1 - frame.c > 0)
    

    and when combined you get

    frame['d'] = frame.b + (frame.c > 0) * (frame.c - frame.b)
    
    0 讨论(0)
  • 2021-02-13 14:36

    is that what you want?

    In [300]: frame[['b','c']].apply(lambda x: x['c'] if x['c']>0 else x['b'], axis=1)
    Out[300]:
    0   -1.099891
    1    0.582815
    2    0.901591
    3    0.900856
    dtype: float64
    
    0 讨论(0)
提交回复
热议问题