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!!

2

1 Answer 1

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

Prints:

{'bill': [80], 'dean': [2, 4, 44, 56], 'mike': [34, 3, 10]}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.