Python np.select不符合我的条件,为什么?

时间:2019-05-07 08:44:25

标签: python pandas margin

我有这部分代码:

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%')

长话短说-我正在尝试计算产品利润率,并根据时间间隔的位置来指定其类型。我的代码正确设置了所有<0边距,但是所有大于或等于零的边距都设置为第二个选项:

  

2)<= 5%

以某种方式只接受第二个条件的第一部分(> = 0),而完全忽略了第二个条件。第三个条件也被完全忽略。为什么会这样?

Active_prods是熊猫数据框。

2 个答案:

答案 0 :(得分:1)

假设active_prods是一个Numpy数组,我认为您的&对于组合逻辑数组是不正确的。

您可能想要:

np.logical_and(active_prods[title] >= 0, active_prods[title] <= 5)

答案 1 :(得分:0)

这是实现它的另一种方法。您可以使用垃圾箱并将范围切成篮子。 在下面看到样机:

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

查看以下结果:

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%
相关问题