6

I have a data frame looks like

    userId  movieId rating
0   12882   1      4.0
1   12882   32     3.5
2   12882   47     5.0
3   12882   50     5.0
4   12882   110    4.5

But I want to convert it into a matrix which the rowname is userId, column name is movieId and the value is the rating.

         1    32      47
12882   4.0   3.5     5.0

I have try to use the groupby, but after that, I have no idea how to convert it.

test = Ratings[['userId','movieId','rating']]

test_group = test.groupby(['userId','movieId'],as_index=False,sort=False)


Nicolas H
  • 535
  • 3
  • 13

1 Answers1

4

You can use DataFrame.pivot for this:

df_pivot = df.pivot(index='userId', columns='movieId', values='rating')

[out]

print(df_pivot)

movieId  1    32   47   50   110
userId                          
12882    4.0  3.5  5.0  5.0  4.5
Chris Adams
  • 18,389
  • 4
  • 22
  • 39