1# For creating new column with multiple conditions
2conditions = [
3 (df['Base Column 1'] == 'A') & (df['Base Column 2'] == 'B'),
4 (df['Base Column 3'] == 'C')]
5choices = ['Conditional Value 1', 'Conditional Value 2']
6df['New Column'] = np.select(conditions, choices, default='Conditional Value 1')
1# np.where(condition, value if condition is true, value if condition is false)
2
3df['hasimage'] = np.where(df['photos']!= '[]', True, False)
4df.head()
1conditions = [
2 df['gender'].eq('male') & df['pet1'].eq(df['pet2']),
3 df['gender'].eq('female') & df['pet1'].isin(['cat', 'dog'])
4]
5
6choices = [5,5]
7
8df['points'] = np.select(conditions, choices, default=0)
9
10print(df)
11 gender pet1 pet2 points
120 male dog dog 5
131 male cat cat 5
142 male dog cat 0
153 female cat squirrel 5
164 female dog dog 5
175 female squirrel cat 0
186 squirrel dog cat 0
19