palantir
palantir

Reputation: 11

Organize unique values in a df column into keys and have the values in the same row copied as their values

I am curious how you would take a df similar to this and turn it into a dictionary with the unique classes as keys and their ages as the values.

Age Class
27 Science
29 English
21 Science
30 math
22 Science
28 math
32 math
18 Science
15 English

Final solution may look like this:

dict = {
  "Science": [27, 21, 22, 18],
  "Math": [30, 28, 32],
  "English": [29, 15]

I assume I have to start with the first class and create a key. Then iterate through the age cells and if their class value in their row is equal to the first key then copy that into the value associated with the key?

Thank you so much for your help!

Upvotes: 0

Views: 30

Answers (2)

BENY
BENY

Reputation: 323326

Try with

df.groupby('Class')['Age'].agg(lambda x : x.unique().tolist()).to_dict()
Out[123]: {'English': [29, 15], 'Science': [27, 21, 22, 18], 'math': [30, 28, 32]}

Upvotes: 0

mcsoini
mcsoini

Reputation: 6642

Assuming that ages are supposed to be unique (i.e. no double ages in the lists), you can simply group by class and get unique values of Age:

df.groupby("Class").Age.unique().apply(list).to_dict()

# out:
{'English': [29, 15], 'Science': [27, 21, 22, 18], 'math': [30, 28, 32]}

If you want to allow for double occurrences of ages (not relevant for your example), you can do something like:

df.groupby("Class").agg(list).Age.to_dict()

Upvotes: 1

Related Questions