12

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?

MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
Bedi Egilmez
  • 1,494
  • 1
  • 18
  • 26

2 Answers2

29

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
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
4

Try list one line (no lambda):

dict(df.groupby('TitleId')['RelatedTitleId'].apply(list))
 # {89989: [32598, 3085083], 95281: [3085083]}
Merlin
  • 24,552
  • 41
  • 131
  • 206