1

Below is an example of my dataframe and a dictionary of what I'm looking to compute from it. My current method involves looping through unique ColA values, create a subset dataframe, getting list of ColB values, and make a dict from that. Problem is I have over a million unique ColA values to loop through. Any ideas??

DF

 ColA       ColB
 mike        34
 mike         3
 mike        10
 bill        80
 dean         2
 dean         4
 dean        44
 dean        56

desired dictionary = {'mike':[34,3,10], 'bill': [10], 'dean': [2,4,44,56]}

Any ideas? Thanks!!

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
dmd7
  • 605
  • 3
  • 8
  • Does this answer your question? [How to group dataframe rows into list in pandas groupby](https://stackoverflow.com/questions/22219004/how-to-group-dataframe-rows-into-list-in-pandas-groupby) – Alexandre B. May 12 '21 at 20:07
  • I'm assuming the value of `'bill'` key should be `[80]` not `[10]` – Andrej Kesely May 12 '21 at 20:08

1 Answers1

3
out = df.groupby("ColA")["ColB"].agg(list).to_dict()
print(out)

Prints:

{'bill': [80], 'dean': [2, 4, 44, 56], 'mike': [34, 3, 10]}
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91