I want to create a new column in a Pandas DataFrame by evaluating multiple conditions in an if-then-else block.
if events.hour <= 6:
events[\'time_slice\
Here's a NumPy approach to it -
tags = ['night','morning','afternoon','evening']
events['time_slice'] = np.take(tags,((events.hour.values-1)//6).clip(min=0))
Sample run -
In [130]: events
Out[130]:
hour time_slice
0 0 night
1 8 morning
2 16 afternoon
3 20 evening
4 2 night
5 14 afternoon
6 7 morning
7 18 afternoon
8 8 morning
9 22 evening