我正在尝试将一些房价类别放在一起,以便将它们叠加在地图上,以显示邻近地区的价格差异。
当我把这个逻辑放在一起把不同的房价放在一起时,我得到的TypeError是:
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吗?
谢谢!
发布于 2020-05-03 01:11:12
出现这个错误是因为您试图将字符串值与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") https://stackoverflow.com/questions/61563265
复制相似问题