使用 zip 形成新的数据框,但出现错误

forming a new data frame using zip, but getting an error

我有一个 numpy_array 叫 playoff_teams:

playoff_teams = np.sort(playoff_seeds['team'])
playoff_teams[:7]

array([1115, 1124, 1139, 1140, 1143, 1155, 1165], dtype=int64)

我有一个 data_frame 叫 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

我该怎么做?

sum() returns a number, not something that you can iterate over while zip() 需要迭代所以我认为你的问题就在那里。

last_six = sum(games.tail(6)['wteam'] == teams)  # Number
zipped = zip(team, last_six)  # Error because last_six is not iterable

您可以将结果存储在列表中(也可能是字典),例如:

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_itemsDataFrame.from_dict 构建数据框(如果您选择的是字典而不是列表)。