Convert dataframe to dictionary of list of tuples

Oktay Gardener

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?

cs95

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)]}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert tuples to list of dictionary

Convert a list of tuples to a dictionary

Convert Dictionary to a list of Tuples

Convert list of tuples into a dictionary with list of tuples

Convert a nested dictionary into list of tuples

How to convert this dictionary into a list of tuples?

Convert nested dictionary into list of tuples

Convert list of Tuples into Dictionary(All tuples convert seperate dictionary)

How to convert dictionary inside a dictionary which contains a list of tuples into a pandas dataframe

Convert pandas dataframe to list of tuples

Convert list of tuples to named dictionary list

Convert a list of dictionary to a a dataframe

How to convert a python dictionary with tuples into a pandas dataframe?

How can I convert a dictionary into a list of tuples?

Convert a list of tuples with repeated keys to a dictionary of lists

How to extract from this list of tuples and convert into this dictionary?

Convert list of tuples with 3 elements into ordered dictionary

How to convert a list of tuples to a dictionary of dictionaries in Python?

How to convert list of tuples to dictionary with index as key

Convert list to dictionary of tuples based on column value

Convert a list of variable length Tuples into Dictionary

How do I convert a list of tuples to a dictionary

Convert a list of tuples to a dictionary, based on tuple values

Convert the tuples in a list to a dictionary and calculate the frequency

python convert list of tuples to composite key dictionary

Transform a Dictionary of Tuples in a List into a Pandas Dataframe

Python convert list of tuples to dictionary with value of multiple tuples

Convert a list of tuples into a dictionary and adding count information as dictionary values

Convert dictionary with list into pandas dataframe