使用zip生成新的数据帧,但是

2024-04-27 18:13:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为playoff_teams的numpu数组:

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

我该怎么做?你知道吗


Tags: 数据npcol数组zipreggamesteam
1条回答
网友
1楼 · 发布于 2024-04-27 18:13:33

^{}返回一个数字,而^{}需要iterables,所以我认为问题就在这里。你知道吗

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_itemsDataFrame.from_dict(如果选择的是dict而不是list)构建数据帧。你知道吗

相关问题 更多 >