I am a long-term SAS user trying to get into Pandas. I would like to set the column value based on various if conditions. I think I can do this using np.where nested commands, but I thought I'd check if there was a more elegant solution. For example, if I set the left bound and right border and want to return a column of string values, if x stays on the left, in the middle or on the right of these borders, what is the best way to do this? Basically, if x <lbound return "left", else if lbound <x <rbound return "middle", else if x> rbound returns "right".
df
lbound rbound x
0 -1 1 0
1 5 7 1
2 0 1 2
One condition can be checked using np.where:
df['area'] = np.where(df['x']>df['rbound'],'right','somewhere else')
But not sure what to do. I want to check multiple if-else ifs on the same line.
The conclusion should be:
df
lbound rbound x area
0 -1 1 0 middle
1 5 7 1 left
2 0 1 2 right
source
share