python - 为重复记录创建新的平均分数 [userid, itemid]

标签 python pandas dataframe duplicates

我有一个如下所示的数据框:

userid  itemid   score
1       5        4
2       3        10
1       5        20
2       3        30

我想将此数据框转换为:

userid  itemid   score
1       5        22
2       3        20

我计划使用 2 个 for 循环来完成此操作。但是,我想知道是否有任何推荐的方法来完成这项任务? groupby 似乎不起作用,因为它没有 average 功能。有什么帮助吗?

最佳答案

尝试使用groupbysum

df.groupby(['userid', 'itemid']).mean()

enter image description here

关于python - 为重复记录创建新的平均分数 [userid, itemid],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40824082/

相关文章:

python - 如何将列的值复制到新列中

python - pandas json_normalize 所有列都有嵌套字典扁平化

python - 导入错误: No module named 'matplotlib'

python - np.where() 消除坐标彼此太接近的数据

python - Django runserver 使用什么类型的服务器?

python - Plotly 中的 Seaborn HUE

python - 在 Pandas 中找到元素时如何查找列号

python - 如何对 Pandas 中的数据进行分组和重新采样?

python - 统计频率,如何每次取两个单词?

python - 根据条件创建列