I have this part of code:
title = "Margin with minimal market price"
active_prods[title] = (active_prods['market min'] - active_prods['cost']) / active_prods['market min']
conditions = [
(active_prods[title] < 0),
(active_prods[title] >= 0) & (active_prods[title] <= 5),
(active_prods[title] > 5) & (active_prods[title] <= 10)]
choices = ['1) <0', '2) <=5%', '3) <=10%']
active_prods['Margin type'] = np.select(conditions, choices, default='4) >10%')
Long story short - I am trying to calculate my product margin and to give it some type depending on where in the interval it is. My code sets all <0 margins correctly, but everything above or equal to zero is set to second option:
2) <=5%
Somehow it only takes the first part of the second condition(which is >=0) and completely ignores the second part of the condition. The third condition is also completely ignored. Why is this happening?
Active_prods is pandas dataframe.
here is another way to achieve it. you can use bin and cut the range into baskets. see a mockup below:
import pandas as pd
active_prods = pd.DataFrame({'Margin':[0,-2,4,10,35,1,54]})
bins= [-100,0,6,11,100]
labels = ['1) <0','2) <=5%','3) <=10%','4) >10%']
active_prods['MarginType'] = pd.cut(active_prods['Margin'], bins=bins, labels=labels, right=False)
active_prods
see the result below:
Margin MarginType
0 0 2) <=5%
1 -2 1) <0
2 4 2) <=5%
3 9 3) <=10%
4 35 4) >10%
5 1 2) <=5%
6 54 4) >10%