我是python的新手,执行以下代码时出现错误。如果有人可以帮助我理解它,我将非常感谢。 关于数据:数据帧存储在“火车”中,列名称为“邻居”。 “邻居”中的值类似于“#Queens#jackson heights”或“#Manhattan#uppereast side”。因此,我尝试拆分主题标签,然后仅考虑每行中的第一个单词(即Queens&Manhattan $等)。 它确实打印了预期的输出,但出现此错误:
IndexError Traceback (most recent call last)
<ipython-input-89-b199ce84fe1c> in <module>()
5 for row in train['neighborhood'].str.split('#'):
6 # if more than a value,
----> 7 if len(row[1]) == 5 :
8 # Append a num grade
9 grades.append('1')
IndexError: list index out of range
train = pd.DataFrame(train, columns = ['id','listing_type','floor','latitude','longitude','price','beds','baths','total_rooms','square_feet','pet_details','neighborhood'])
# Create a list to store the data
grades = [ ]
# For each row in the column,
for row in train['neighborhood'].str.split('#'):
# if more than a value,
if row[1] == 'Queens':
# Append a num grade
grades.append('1')
# else, if more than a value,
elif row[1] == 'Manhattan':
# Append a letter grade
grades.append('2')
# else, if more than a value,
elif row[1] == 'Bronx':
# Append a letter grade
grades.append('3')
# else, if more than a value,
elif row[1] == 'Brooklyn':
# Append a letter grade
grades.append('4')
# else, if more than a value,
else:
# Append a fail0ing grade
grades.append('5')