Pandas中的UnboundLocalError

时间:2017-09-07 12:37:13

标签: pandas

我的任务是清理数据并以下一格式呈现:DataFrame([[" Michigan"," Ann Arbor"],[" Michigan" ," Yipsilanti"]],columns = [" State"," RegionName"])。

我犯了一个错误。我该如何解决? " UnboundLocalError:局部变量' state'在分配之前引用"

代码如下:

 with open ('university_towns.txt') as file:
    data=[]
    for line in file:
        data.append(line[:-1])
state_town = []

for line in data:
    if line[:-6]=='[edit]':
        state=line[:-6]
    elif '(' in line:
        town = line[:line.index('(')-1]
        state_town.append([state,town])
    else:
        town=line
        state_town.append([state,town])
    state_college_df=pd.DataFrame(state_town, columns=['State','RegionName'])
return state_college_df

0 个答案:

没有答案