首页 > 解决方案 > 重新计算和更正numpy数组中的值

问题描述

我有一个 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 中)来创建一个具有更正分数的新数组?

标签: pythonpandasnumpycsvnumpy-ndarray

解决方案


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

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

结果df:

在此处输入图像描述


推荐阅读