【问题标题】:re-calculating and correcting values inside numpy array重新计算和更正numpy数组中的值
【发布时间】:2021-11-13 13:11:18
【问题描述】:

我有一个 CSV 格式的数据集,作为二维数组导入:

game_id, player_score, rounds_played
0, 56, 30
0, 44, 30
1, 77, 26
1, 34, 26
2, 36, 23
2, 31, 23

在下一步中,我需要创建一个新数组,其中 player_score 需要与每场比赛中的 rounds_played 相对化,以估计单独查看分数时的表现。

在“for 循环”中,我做了类似的事情,我计算了每个分数的“权重”与所玩的回合,然后再次将因子乘以回合:

假设 30 是这个集合中的最大 rounds_played 值:

weight = (player_score * rounds_played) / 30
new_score = weight

然后我会将 new_score 附加到一个新数组中。

如果我有一个更大的二维数组,这可能会变得很慢。 - 有没有更短、更直接的方法(可能是在 numpy 中)来创建一个具有更正分数的新数组?

【问题讨论】:

    标签: python pandas numpy csv numpy-ndarray


    【解决方案1】:

    您可以在 pandas 中轻松快速地完成此操作(感谢 python 矢量化)。

    df['new_score'] = (df.player_score * df.rounds_played) / 30
    

    结果df:

    【讨论】:

      猜你喜欢
      • 2019-02-21
      • 2021-07-07
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多