.Split()在for循环中没有返回列表

时间:2017-08-02 18:02:12

标签: python python-3.x pandas

我写了一个for循环,它应该取得足球队的胜负记录并将其拆分,以获得赢得比赛和输掉比赛的价值。不幸的是,在我写的for循环中使用时,我的split(' - ')命令似乎没有返回列表。

从维基百科中获取数据集,数据在熊猫数据框中。

以下是我获取数据框的方法:

test = pd.read_html('https://en.m.wikipedia.org/wiki/
    List_of_Michigan_Wolverines_football_seasons')

year_football = test[-1].T.set_index(0).T.dropna(axis=0, thresh=3)

我想要迭代的列表是:

#format W-L
win_loss = ['7–1' '6–2' '2–6' '1–7' '3–5' '6–2' '6–2' '3–5' '3–5' '6–2' '7–2']

我通过做一些清洁然后打电话来获得:

print(year_football['Conference'].values)

我的循环是:

wins = []
games = []

for season in year_football['Conference'].values:
        win_loss = season.split('-')
        wins.append(win_loss[0])
        games.append(int(win_loss[0])) + int(win_loss[1]))
        print(season)
        print(type(season))
        print(win_loss)

列表第一个成员的输出是:

7–1 #print(season)
<class 'str'> #print(type(season))
['7–1'] #print(win_loss)

我无法弄清楚我做错了什么,.split()在for循环之外工作正常。希望不是拼写错误。 (如果有帮助的话,也可以在Jupyter中运行)

1 个答案:

答案 0 :(得分:4)

将for循环更改为拆分该实际角色

for season in year_football['Conference'].values:
        win_loss = season.split(chr(8211))  # I changed this line
        wins.append(win_loss[0])
        games.append(int(win_loss[0])) + int(win_loss[1]))
        print(season)
        print(type(season))
        print(win_loss)
相关问题