Create Column with ELIF in Pandas

In cases where you have multiple branching statements it’s best to create a function that accepts a row and then apply it along the axis=1. This is usually much faster then iteration through rows.

def func(row):
    if row['mobile'] == 'mobile':
        return 'mobile'
    elif row['tablet'] =='tablet':
        return 'tablet' 
    else:
        return 'other'

df['combo'] = df.apply(func, axis=1)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)