我有一个名为playoff_teams
的numpy_array:
playoff_teams = np.sort(playoff_seeds['team'])
playoff_teams[:7]
array([1115, 1124, 1139, 1140, 1143, 1155, 1165], dtype=int64)
我有一个名为reg
的数据框:
season daynum wteam wscore lteam lscore wloc numot
108122 2010 7 1143 75 1293 70 H 0
108123 2010 7 1314 88 1198 72 H 0
108124 2010 7 1326 100 1108 60 H 0
108125 2010 7 1393 75 1107 43 H 0
108126 2010 9 1143 95 1178 61 H 0
然后我循环遍历团队并执行以下操作:
for teams in playoff_teams:
games = reg[(reg['wteam'] == teams) | (reg['lteam']== teams)]
last_six = sum(games.tail(6)['wteam'] == teams)
zipped = zip(team, last_six)
我收到错误
TypeError: zip argument #1 must support iteration
我需要以下列格式形成一个新的数据框:
col_1 col_2
team_1 last_six
team_2 last_six
team_3 last_six
我该怎么做?
答案 0 :(得分:2)
sum()
返回一个数字,而不是你可以迭代的东西,而zip()
需要迭代,所以我认为你的问题就在那里。
last_six = sum(games.tail(6)['wteam'] == teams) # Number
zipped = zip(team, last_six) # Error because last_six is not iterable
您可以将结果存储在列表中(也可能是dict),例如:
new_data = []
for teams in playoff_teams:
games = reg[(reg['wteam'] == teams) | (reg['lteam']== teams)]
last_six = sum(games.tail(6)['wteam'] == teams)
new_data.append((teams, last_six))
然后使用DataFrame.from_items
或DataFrame.from_dict
构建您的数据框(如果您选择了dict而不是列表)。