I am trying to fill a column based on condition but something goes wrong.
So I am expecting if it is not 2017 there should be a no.
But yet the years 2019 and 2020 also get filled up with yes.
Does anyone know what went wrong?
`` dfV['is_vacation'] = np.where((dfV['year'] == 2017) & (dfV['week_nr'] == 26) & (dfV['week_nr'] == 27) & (dfV['week_nr'] == 28) & (dfV['week_nr'] == 29) & (dfV['week_nr'] == 30) & (dfV['week_nr'] == 31) & (dfV['week_nr'] == 32) & (dfV['week_nr'] == 33) & (dfV['week_nr'] == 34) & (dfV['week_nr'] == 35) & (dfV['week_nr'] == 42) & (dfV['week_nr'] == 43) & (dfV['week_nr'] == 44), 'Yes', 'No') `` ``
Advertisement
Answer
Use Series.between
instead &
for bitwise AND
, for chain by bitwise OR
use |
:
dfV = pd.DataFrame({'year':[2017,2018,2019,2017,2017,2017], 'week_nr':[43,30,20,40,30,36]}) dfV['is_vacation'] = np.where((dfV['year'] == 2017) & (dfV['week_nr'].between(26, 35) | dfV['week_nr'].between(42, 44)), 'Yes', 'No') print (dfV) year week_nr is_vacation 0 2017 43 Yes 1 2018 30 No 2 2019 20 No 3 2017 40 No 4 2017 30 Yes 5 2017 36 No
Or:
L = list(range(26, 36)) + list(range(42, 45)) dfV['is_vacation'] = np.where((dfV['year'] == 2017) & dfV['week_nr'].isin(L), 'Yes', 'No')