2

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
1
  • your code works for me - additionaly it returns 0 because that's teh default - try np.select(conditions, choices,default=df['bonus']) Commented Apr 16, 2020 at 18:40

3 Answers 3

3

This will be easy using numpy.clip:

import numpy as np

df['bonus_capped'] = np.clip(df['bonus'], 0.8, 1.2)

where 0.8 and 1.2 are your lower and upper limits respectively.

Sign up to request clarification or add additional context in comments.

Comments

2

In pandas

upper_limit = 1.2
lower_limit = 0.8
df.bonus.clip(lower_limit,upper_limit)
0    1.20
1    1.12
2    1.09
3    0.90
4    0.80
5    0.83
Name: bonus, dtype: float64
#df.bonus=df.bonus.clip(lower_limit,upper_limit)

Comments

1

By going with the method you were trying:

In [1100]: col         = 'bonus' 
      ...: conditions  = [ df['bonus'] < 0.8 , df['bonus'] > 1.2] 
      ...: choices     = [0.8, 1.2]                                                                                                                                                                         

In [1102]: df['bonus_capped'] = np.select(conditions, choices, default=df['bonus'])                                                                                                                         

In [1103]: df                                                                                                                                                                                               
Out[1103]: 
   id  bonus  bonus_capped
0   1   1.50          1.20
1   2   1.12          1.12
2   3   1.09          1.09
3   4   0.9           0.9
4   5   0.74          0.80
5   6   0.83          0.83

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.