python - Create Column with ELIF in Pandas -


question

i having trouble figuring out how create new dataframe column based on values in 2 other columns. need use if/elif/else logic. of documentation , examples have found show if/else logic. here sample of trying do:

code

df['combo'] = 'mobile' if (df['mobile'] == 'mobile') elif (df['tablet'] =='tablet') 'tablet' else 'other') 

i open using where() also. having trouble finding right syntax.

in cases have multiple branching statements it's best create function accepts row , apply along axis=1. faster 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) 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -