3

I have a dataframe like this,

Roll No     date     status     Name
1           1/1/2020  on         A
2           1/1/2020  on         A
3           1/1/2020  on         B

I am trying to create a dictionary where key will be the names and value will be list of roll numbers, I am unable to handle the duplicate name,

my expected output is,

{
    "A" :[1,2],
    "B" :[3]
}

I can get my output by iterating the dataframe, I am looking for a pandorable way, thanks.

Pyd
  • 6,017
  • 18
  • 52
  • 109

1 Answers1

5

Use DataFrame.groupby with convert to lists in GroupBy.agg and then Series.to_dict:

d = df.groupby('Name')['Roll No'].agg(list).to_dict()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252