renakre renakre - 13 days ago 6
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?
groupby
does not seem to work since it does not have
average
function. Any help?

Answer

try to use groupby and sum

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

enter image description here