使用python的逻辑语句将房价分门别类。

问题描述 投票:0回答:1

我试图将一些房屋价格类别进行分类,以便将它们叠加在地图上,以显示社区的价格差异。

当我把这个逻辑组合在一起,把不同的住房价格分类时,我得到了一个TypeError。

类型错误:"str "和 "int "实例之间不支持"<

代码如下。

level = []
for i in range(0,len(data_process)):
    if (data_process['HousingCost'][i] < 150000):
        level.append("Low Level Cost")
    elif (data_process['HousingCost'][i] >= 150001 and data_process['HousingCost'][i] < 300000):
        level.append("Mid-1 Level Cost")
    elif (data_process['HousingCost'][i] >= 300001 and data_process['HousingCost'][i] < 450000):
        level.append("Mid-2 Level Cost")
    elif (data_process['HousingCost'][i] >= 450001 and data_process['HousingCost'][i] < 600000):
        level.append("High-1 Level Cost")
    else:
        level.append("High-2 Level Cost")   

data_process['Level_labels'] = level
data_process.head()

我不知道为什么我得到这个类型错误,因为我认为我的结构是正确的。

请帮助我纠正这个TypeError。

谢谢你的帮助

python logic binning
1个回答
0
投票

发生错误的原因是你试图比较字符串值和int值,可能是由于访问的值里面的 data_process 是字符串。

试着将 data_process 访问的值改为int。例如:

level = []
for i in range(0,len(data_process)):
    housing_cost = int(data_process['HousingCost'][i])
    if housing_cost < 150000:
        level.append("Low Level Cost")
    elif housing_cost >= 150001 and housing_cost < 300000:
        level.append("Mid-1 Level Cost")
    elif housing_cost >= 300001 and housing_cost < 450000:
        level.append("Mid-2 Level Cost")
    elif housing_cost >= 450001 and housing_cost < 600000:
        level.append("High-1 Level Cost")
    else:
        level.append("High-2 Level Cost")   
© www.soinside.com 2019 - 2024. All rights reserved.