multiple if else conditions in pandas dataframe and derive multiple columns

前端 未结 3 710
有刺的猬
有刺的猬 2020-12-05 21:30

I have a dataframe like below.

import pandas as pd
import numpy as np
raw_data = {\'student\':[\'A\',\'B\',\'C\',\'D\',\'E\'],
        \'score\': [100, 96, 8         


        
相关标签:
3条回答
  • 2020-12-05 22:03

    you can use also apply with a custom function on axis 1 like this :

    def color_selector(x):
        if (x['trigger1'] <= x['score'] < x['trigger2']) and (x['height'] < 8):
            return 'Red'
        elif (x['trigger2'] <= x['score'] < x['trigger3']) and (x['height'] < 8):
            return 'Yellow'
        elif (x['trigger3'] <= x['score']) and (x['height'] < 8):
            return 'Orange'
        elif (x['height'] > 8):
            return ''
    df2 = df2.assign(flag=df2.apply(color_selector, axis=1))
    

    you will get something like this :

    0 讨论(0)
  • 2020-12-05 22:04

    Here is a way to use numpy.select() for doing this in a non cluttered way:

    conditions = [
        (df2['trigger1'] <= df2['score']) & (df2['score'] < df2['trigger2']) & (df2['height'] < 8),
        (df2['trigger2'] <= df2['score']) & (df2['score'] < df2['trigger3']) & (df2['height'] < 8),
        (df2['trigger3'] <= df2['score']) & (df2['height'] < 8),
        (df2['height'] > 8)
    ]
    
    choices = ['Red','Yellow','Orange', np.nan]
    df['Flag1'] = np.select(conditions, choices, default=np.nan)
    
    0 讨论(0)
  • 2020-12-05 22:07

    You need chained comparison using upper and lower bound

    def flag_df(df):
    
        if (df['trigger1'] <= df['score'] < df['trigger2']) and (df['height'] < 8):
            return 'Red'
        elif (df['trigger2'] <= df['score'] < df['trigger3']) and (df['height'] < 8):
            return 'Yellow'
        elif (df['trigger3'] <= df['score']) and (df['height'] < 8):
            return 'Orange'
        elif (df['height'] > 8):
            return np.nan
    
    df2['Flag'] = df2.apply(flag_df, axis = 1)
    
        student score   height  trigger1    trigger2    trigger3    Flag
    0   A       100     7       84          99          114         Yellow
    1   B       96      4       95          110         125         Red
    2   C       80      9       15          30          45          NaN
    3   D       105     5       78          93          108         Yellow
    4   E       156     3       16          31          46          Orange
    

    Note: You can do this with a very nested np.where but I prefer to apply a function for multiple if-else

    0 讨论(0)
提交回复
热议问题