renakre renakre - 9 months ago 46
Python Question

creating a new average score for the duplicate records [userid, itemid]

I have a dataframe that looks like this:

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

I want to convert this dataframe to:

userid itemid score
1 5 22
2 3 20

I am planning to do this using 2 for loops. However, I wonder if there is any recommended approach for achieving this task?
does not seem to work since it does not have
function. Any help?

Answer Source

try to use groupby and sum

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

enter image description here