Numpy“哪里"有多个条件 [英] Numpy "where" with multiple conditions

查看:32
本文介绍了Numpy“哪里"有多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试向数据框df_energy"添加一个新列energy_class",如果consumption_energy"值> 400,则它包含字符串high",如果consumption_energy"值在200和400之间,则添加medium",如果consumption_energy"值低于 200,则为低".我尝试使用 numpy 中的 np.where,但我看到 numpy.where(condition[, x, y]) 只处理两个条件,而不是 3 就像我的情况.

I try to add a new column "energy_class" to a dataframe "df_energy" which it contains the string "high" if the "consumption_energy" value > 400, "medium" if the "consumption_energy" value is between 200 and 400, and "low" if the "consumption_energy" value is under 200. I try to use np.where from numpy, but I see that numpy.where(condition[, x, y]) treat only two condition not 3 like in my case.

有什么想法可以帮助我吗?

Any idea to help me please?

提前致谢

推荐答案

您可以使用 三元:

np.where(consumption_energy > 400, 'high', 
         (np.where(consumption_energy < 200, 'low', 'medium')))

这篇关于Numpy“哪里"有多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆