Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas groupby two columns then get dict for values

I have a pandas dataframe:

banned_titles = 
TitleId  RelatedTitleId
0    89989           32598
1    89989         3085083
2    95281         3085083

when I apply groupby as following

In [84]: banned_titles.groupby('TitleId').groups
Out[84]: {89989: [0, 1], 95281: [2]}

This is so close but not I want.

What I want is:

{89989: [32598, 3085083], 95281: [3085083]}

Is there a way to do this?

like image 693
Bedi Egilmez Avatar asked Jun 07 '16 21:06

Bedi Egilmez


2 Answers

try this:

In [8]: x.groupby('TitleId')['RelatedTitleId'].apply(lambda x: x.tolist()).to_dict()
Out[8]: {89989: [32598, 3085083], 95281: [3085083]}

or as series of lists:

In [10]: x.groupby('TitleId')['RelatedTitleId'].apply(lambda x: x.tolist())
Out[10]:
TitleId
89989    [32598, 3085083]
95281           [3085083]
Name: RelatedTitleId, dtype: object

data:

In [9]: x
Out[9]:
   TitleId  RelatedTitleId
0    89989           32598
1    89989         3085083
2    95281         3085083
like image 117
MaxU - stop WAR against UA Avatar answered Sep 30 '22 01:09

MaxU - stop WAR against UA


Try list one line (no lambda):

dict(df.groupby('TitleId')['RelatedTitleId'].apply(list))
 # {89989: [32598, 3085083], 95281: [3085083]}
like image 29
Merlin Avatar answered Sep 30 '22 03:09

Merlin