Alfred
Alfred

Reputation: 43

How to transform dataframe to dict in Python3

I have been searching for a long time on the net but to no avail. Please help or try to give some ideas how to achieve this

I use pandas to read MovieLens csv file

ratings = pd.read_table('ml-latest-small/ratings.csv')

then I get a table like this:

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 transform it to dict like

{userId:{movieId:rating}}

e.g

{
 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 failed:

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 someone please help me?

Upvotes: 4

Views: 253

Answers (1)

jezrael
jezrael

Reputation: 862511

You can use groupby with 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 deleted 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}
}

Upvotes: 4

Related Questions