How to compare two columns of the same dataframe?

后端 未结 3 573
無奈伤痛
無奈伤痛 2021-01-05 07:22

I have a dataframe like this:

 match_id inn1  bat  bowl  runs1 inn2   runs2   is_score_chased
    1     1     KKR  RCB    222  2      82          1
    2             


        
3条回答
  •  无人及你
    2021-01-05 07:27

    This is a good case for using apply.

    Here there is an example of using apply on two columns.

    You can adapt it to your question with this:

    def f(x):    
       return 'yes' if x['run1'] > x['run2'] else 'no'
    
    df['is_score_chased'] = df.apply(f, axis=1)
    

    However, I would suggest filling your column with booleans so you can make it more simple

    def f(x):    
       return x['run1'] > x['run2']
    

    And also using lambdas so you make it in one line

    df['is_score_chased'] = df.apply(lambda x: x['run1'] > x['run2'], axis=1)
    

提交回复
热议问题