numpy的“其中”与多个条件
问题描述:
我尝试添加新的列“ENERGY_CLASS”发送数据帧“df_energy”它包含字符串“高”如果“consumption_energy”值> 400,“中”如果“consumption_energy “价值是200和400之间,以及‘低’,如果‘consumption_energy’值低于200 我尝试从numpy的np.where使用,但我看到numpy.where(condition[, x, y])
在我的情况下治疗只有两个条件不一样3。numpy的“其中”与多个条件
任何想法,以帮助我吗?
预先感谢您
答
您可以使用ternary:
np.where(consumption_energy > 400, 'high',
(np.where(consumption_energy < 200, 'low', 'medium)))
答
我会在这里使用的cut()方法,这将产生非常高效和节省内存的category
D型:
In [124]: df
Out[124]:
consumption_energy
0 459
1 416
2 186
3 250
4 411
5 210
6 343
7 328
8 208
9 223
In [125]: pd.cut(df.consumption_energy, [0, 200, 400, np.inf], labels=['low','medium','high'])
Out[125]:
0 high
1 high
2 low
3 medium
4 high
5 medium
6 medium
7 medium
8 medium
9 medium
Name: consumption_energy, dtype: category
Categories (3, object): [low < medium < high]
答
试试这个: 使用@Maxu的设置
conditions = [ df2['consumption_energy'] >= 400, (df2['consumption_energy'] < 400) & (df2['consumption_energy']> 200), df2['consumption_energy'] <= 200 ]
choices = [ "high", 'medium', 'low' ]
df2["energy_class"] = np.select(conditions, choices, default=np.nan)
consumption_energy energy_class
0 459 high
1 416 high
2 186 low
3 250 medium
4 411 high
5 210 medium
6 343 medium
7 328 medium
8 208 medium
9 223 medium
答
我喜欢保持代码的清洁。这就是为什么我喜欢np.vectorize
这样的任务。
def conditions(x):
if x > 400:
return "High"
elif x > 200:
return "Medium"
else:
return "Low"
func = np.vectorize(conditions)
energy_class = func(df_energy["consumption_energy"])
然后,只需使用添加numpy的阵列在数据帧的列:
df_energy["energy_class"] = energy_class
这种方法的好处是,如果你想更复杂的约束添加到列,这是可以做到容易。 希望它有帮助。