Question
I am having trouble figuring out how to create new DataFrame column based on the values in two other columns. I need to use if/elif/else l
ELIF logic can be implemented with np.select or nested np.where:
import numpy as np
df['combo'] = np.select([df.mobile == 'mobile', df.tablet == 'tablet'],
['mobile', 'tablet'],
default='other')
# or
df['combo'] = np.where(df.mobile == 'mobile', 'mobile',
np.where(df.tablet == 'tablet', 'tablet', 'other'))
mobile tablet combo
0 mobile bar mobile
1 foo tablet tablet
2 foo nan other
3 mobile tablet mobile
4 mobile nan mobile
5 foo tablet tablet
6 mobile bar mobile
7 mobile tablet mobile
8 mobile bar mobile
9 mobile nan mobile