Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If ElseIf Else condition in pandas dataframe list comprehension

Tags:

python

pandas

I have a dataframe with 11 columns: Status1-Status5, Time1-Time5 & Time_Min

df = pd.DataFrame([[100,200,150,400,500,'a','b','a','c','a',100], [300,400,200,500,250,'b','b','c','c','c',200]], columns=['TIME_1', 'TIME_2', 'TIME_3', 'TIME_4', 'TIME_5','STATUS_1','STATUS_2','STATUS_3','STATUS_4','STATUS_5','TIME_MIN'])

I would like to reproduce a code I have in SAS currently which does the following

IF TIME_1 = TIME_MIN THEN STATUS = STATUS_1;
ELSE IF TIME_2 = TIME_MIN THEN STATUS = STATUS_2;
ELSE IF TIME_3 = TIME_MIN THEN STATUS = STATUS_3;
ELSE IF TIME_4 = TIME_MIN THEN STATUS = STATUS_4;
ELSE STATUS = STATUS_5;

Expected output for column STATUS would be

['a','c']

I tried building something along these lines (which would need to be extended with else ifs)

df['STATUS'] = [a if x == y else b for x,y,a,b in df[['TIME_MIN','TIME_1','STATUS_1','STATUS_2']]]

But this just gives an error. I'm sure it's a simple fix, but I can't quite figure it out.

like image 423
DarknessFalls Avatar asked Jan 30 '18 17:01

DarknessFalls


2 Answers

You can write a function

def get_status(df):
    if df['TIME_1'] == df['TIME_MIN']:
        return df['STATUS_1']
    elif df['TIME_2'] == df['TIME_MIN']:
        return df['STATUS_2']
    elif df['TIME_3'] == df['TIME_MIN']:
        return df['STATUS_3']
    elif df['TIME_4'] == df['TIME_MIN']:
        return df['STATUS_4']
    else:
        return df['STATUS_5']

df['STATUS'] = df.apply(get_status, axis = 1)

Or use a very-nested np.where,

df['STATUS'] = np.where(df['TIME_1'] == df['TIME_MIN'], df['STATUS_1'],\ 
        np.where(df['TIME_2'] == df['TIME_MIN'], df['STATUS_2'],\
        np.where(df['TIME_3'] == df['TIME_MIN'], df['STATUS_3'],\
        np.where(df['TIME_4'] == df['TIME_MIN'], df['STATUS_4'], df['STATUS_5']))))
like image 154
Vaishali Avatar answered Sep 26 '22 02:09

Vaishali


Not very pretty but you can use equality broadcasting with the .eq method.

m = df.iloc[:, :5].eq(df['TIME_MIN'], axis=0)
m.columns = 'STATUS_' + m.columns.str.extract('TIME_(.*)')
df['STATUS'] = df[m].bfill(axis=1).iloc[:, 0]
like image 29
Alex Avatar answered Sep 25 '22 02:09

Alex