脾气暴躁的“哪里"有多个条件 [英] Numpy "where" with multiple conditions

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

问题描述

我尝试将新列"energy_class"添加到数据框"df_energy",如果"consumption_energy"值> 400,则包含字符串"high";如果"consumption_energy"值在200至400之间,则添加"medium" ,如果"consumption_energy"值小于200,则为低". 我尝试使用numpy中的np.where,但是我发现numpy.where(condition[, x, y])仅处理两个条件而不是3.

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

提前谢谢

解决方案

您可以使用 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?

Thank you in advance

解决方案

You can use a ternary:

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

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

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