Creating a new variable with multiple conditions in python



I am trying to create a new variable in a data frame by applying conditions on two other variable but it is giving me errorm, similar approach would have worked on R :
train['day_type']='' train.day_type[(train['holiday']==1 & train['workingday']==0)]='holiday'

it is giving me the following error :

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

but when i try to use the above code by applying the condition on only one variable it works :
train['day_type']='' train.day_type[(train['holiday']==1 )]='holiday'

P.S. its working just fine with only one comparison using (.ix )or not but when i try to use two comparison by using and ,or it gives out error.

please help !!


Hi syed,

Try this

train.loc[(train['holiday']==1) & (train['workingday']==0), 'day_type'] = 'holiday'

Ankit Gupta