2

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
  • 5,668
  • 14
  • 46
  • 94

1 Answers1

4

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
  • 729,927
  • 78
  • 1,141
  • 1,090