小编par*_*rno的帖子

Python:在 pd.DataFrame 中循环行时,“ValueError:只能将大小为 1 的数组转换为 Python 标量”

我想遍历 DataFrame 的行,在我的例子中计算多个运动队的强度等级。

DataFrame 列'home_elo''away_elo'包含所涉及球队的赛前实力评级(ELO 分数),并在比赛结束后在下一场主/客场比赛的行中更新(每支球队在任何时间点都有两个实力评级,对于主场)和客场比赛),什么update_elo(a,b,c)回报。

相应的代码片段如下所示:

for index in df.index:

    counter = counter + 1
    # Calculation of post-match ELO scores for home and away teams
    if df.at[index,'updated'] == 2: # Update next match ELO scores if not yet updated but pre-match ELO scores available

        try:
            all_home_fixtures = df.date_rank[df['localteam_id'] == df.at[index,'localteam_id']]
            next_home_fixture = all_home_fixtures[all_home_fixtures > df.at[index,'date_rank']].min()
            next_home_index = df[(df['date_rank'] == next_home_fixture) & (df['localteam_id'] == df.at[index,'localteam_id'])].index.item()
        except ValueError:
            print('ERROR 1 at' + str(index))
            df.at[index,'updated'] …
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

8
推荐指数
2
解决办法
3万
查看次数

标签 统计

dataframe ×1

pandas ×1

python ×1