Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert dataframe to dictionary of list of tuples

I have a dataframe that looks like the following

    user                             item  \
0  b80344d063b5ccb3212f76538f3d9e43d87dca9e          The Cove - Jack Johnson   
1  b80344d063b5ccb3212f76538f3d9e43d87dca9e  Entre Dos Aguas - Paco De Lucia   
2  b80344d063b5ccb3212f76538f3d9e43d87dca9e            Stronger - Kanye West   
3  b80344d063b5ccb3212f76538f3d9e43d87dca9e    Constellations - Jack Johnson   
4  b80344d063b5ccb3212f76538f3d9e43d87dca9e      Learn To Fly - Foo Fighters   

rating  
0       1  
1       2  
2       1  
3       1  
4       1  

and would like to achieve the following structure:

dict-> list of tuples
user-> (item, rating)

b80344d063b5ccb3212f76538f3d9e43d87dca9e -> list((The Cove - Jack 
Johnson, 1), ... , )

I can do:

item_set = dict((user, set(items)) for user, items in \
data.groupby('user')['item'])

But that only gets me halfways. How do I get the corresponding "rating" value from the groupby?

like image 400
Oktay Gardener Avatar asked Aug 25 '17 13:08

Oktay Gardener


1 Answers

Set user as index, convert to tuple using df.apply, groupby index using df.groupby(level=0) and get a list using dfGroupBy.agg and convert to dictionary using df.to_dict:

In [1417]: df
Out[1417]: 
                                       user                             item  \
0  b80344d063b5ccb3212f76538f3d9e43d87dca9e          The Cove - Jack Johnson   
1  b80344d063b5ccb3212f76538f3d9e43d87dca9e  Entre Dos Aguas - Paco De Lucia   
2  b80344d063b5ccb3212f76538f3d9e43d87dca9e            Stronger - Kanye West   
3  b80344d063b5ccb3212f76538f3d9e43d87dca9e    Constellations - Jack Johnson   
4  b80344d063b5ccb3212f76538f3d9e43d87dca9e      Learn To Fly - Foo Fighters   

   rating  
0       1  
1       2  
2       2  
3       2  
4       2  

In [1418]: df.set_index('user').apply(tuple, 1)\
             .groupby(level=0).agg(lambda x: list(x.values))\
             .to_dict()
Out[1418]: 
{'b80344d063b5ccb3212f76538f3d9e43d87dca9e': [('The Cove - Jack Johnson', 1),
  ('Entre Dos Aguas - Paco De Lucia', 2),
  ('Stronger - Kanye West', 2),
  ('Constellations - Jack Johnson', 2),
  ('Learn To Fly - Foo Fighters', 2)]}
like image 185
cs95 Avatar answered Oct 22 '22 15:10

cs95