How to convert dataframe to dict in Python3

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?

+4
source share
1 answer

You can use groupbywith iterrows:

d = df.groupby('userId').apply(lambda y: {int(x.movieId): x.rating for i, x in y.iterrows()})
      .to_dict()
print (d)
{
1: {1129: 2.0, 1061: 3.0, 1172: 4.0, 1029: 3.0, 31: 2.5}, 
2: {1111: 4.5, 31: 3.0}
}

Another solution from a remote answer:

d1 = df.groupby('userId').apply(lambda x: dict(zip(x['movieId'], x['rating']))).to_dict()
print (d1)
{
1: {1129: 2.0, 1061: 3.0, 1172: 4.0, 1029: 3.0, 31: 2.5}, 
2: {1111: 4.5, 31: 3.0}
}
+4
source

Source: https://habr.com/ru/post/1665525/


All Articles