Apologies for the noob question but I struggle with python conditionals.
Have the following dataframe:
id bonus
1 1.5
2 1.12
3 1.09
4 0.9
5 0.74
6 0.83
I have upper and lower limit variables:
upper_limit = 1.2
lower limit = 0.8
Trying to write a conditional that: 1) Checks if the bonus is above or below the threshold 2) Creates a new column that ensures the value doesn't go above or below the thresholds. If the bonus value is within range, it doesn't change.
Should look like:
id bonus bonus_capped
1 1.5 1.2
2 1.12 1.12
3 1.09 1.09
4 0.9 0.9
5 0.74 0.8
6 0.83 0.83
My code is:
conditions = [df["bonus"] > upper_limit, df["bonus"] < lower_limit]
choices = [upper_limit, lower_limit]
df["bonus_capped"] = np.select(conditions, choices)
print(df)
but the output I'm getting is only addressing one condition and returning zeros for the rest. What am I missing?
id bonus bonus_capped
1 1.5 0
2 1.12 0
3 1.09 0
4 0.9 0
5 0.74 0.8
6 0.83 0
np.select(conditions, choices,default=df['bonus'])