I searched the net for a long time, but to no avail. Please help or try to give some ideas on how to achieve this.
I use pandas to read csv file of MovieLens
ratings = pd.read_table('ml-latest-small/ratings.csv')
then I get the table as follows:
userId movieId rating timestamp
1 31 2.5 1260759144
1 1029 3.0 1260759179
1 1061 3.0 1260759182
1 1129 2.0 1260759185
1 1172 4.0 1260759205
2 31 3.0 1260759134
2 1111 4.5 1260759256
I want to convert it to a dict, for example
{userId:{movieId:rating}}
eg
{
1:{31:2.5,1029:3.0,1061,3.0,1129:2.0,1172:4.0},
2:{31:3.0,1111:4.5}
}
I tried this code but could not:
for user in ratings['userId']:
for movieid in ratings['movieId']:
di_rating.setdefault(user,{})
di_rating[user][movieid]=ratings['rating'][ratings['userId'] == user][ratings['movieId'] == movieid]
Can anyone help me out?
source
share