I have a df like this:
ID Cluster Product
1 4 'b'
1 4 'f'
1 4 'w'
2 7 'u'
2 7 'b'
3 5 'h'
3 5 'f'
3 5 'm'
3 5 'd'
4 7 's'
4 7 'b'
4 7 'g'
Where ID is the primary and unique key of another df that is the source for this df. Cluster is not a key, different IDs often have same Cluster value; anyway it's an information I have to carry on.
What I want to obtain is this dataframe:
ID Cluster Product_List_by_ID
1 4 ['b','f','w']
2 7 ['u','b']
3 5 ['h','f','m','d']
4 7 ['s','b','g']
If this is not possible, also a dictionary like this could be fine:
d = {ID:[1,2,3,4], Cluster:[4,7,5,7],
Product_List_by_ID:[['b','f','w'],['u','b'],['h','f','m','d'],['s','b','g']]}
I have tried many ways unsuccessfully.. it seems that it is not possible to insert lists as pandas dataframe values.. Anyway I think it should not be so difficult to get the goal in some tricky way.. Sorry if I am going out of mind, but I am new to coding
Any suggests?! Thanks
use
groupby