问题
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)
回答1:
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
来源:https://stackoverflow.com/questions/55764520/how-to-convert-data-frame-into-matrix-based-on-two-columns-in-data-frame