Skip to content Skip to sidebar Skip to footer

Numpy Where With More Than 2 Conditions

Good Morning, I have the following a dataframe with two columns of integers and a Series (diff) computed as: diff = (df['col_1'] - df['col_2']) / (df['col_2']) I would like to cr

Solution 1:

You can use numpy.select to specify conditions and values separately.

s = (df['col_1'] / df['col_2']) - 1

conditions = [s.between(0, 0.35), s > 0.35, s.between(-0.35, 0), s < -0.35]
values = [0, 1, 2, 3]

df['Class'] = np.select(conditions, values, np.nan)

Solution 2:

One can also simply use numpy.searchsorted:

diff_classes = [-0.35,0,0.35]
defgetClass(x):
    returnlen(diff_classes)-np.searchsorted(diff_classes,x)

df["class"]=diff.apply(getClass)

searchsorted will give you the index of x in the diff_classes list, which you then substract from 3 to get your desired result.

edit: A little bit less readable, but it also works in one line:

df["class"] = diff.apply(lambda x: 3-np.searchsorted([-0.35,0,0.35],x))

Post a Comment for "Numpy Where With More Than 2 Conditions"