Reputation: 1142
I am trying to create a dictionary for the following dataset usin:
id value
1 a
1 b
1 c
2 e
2 f
2 g
3 h
3 g
3 l
The output should be like this
{1: [a,b,c], 2:[e, f, g], 3: [h, g, l]}
I know some references on how create dictionary, but none of them give such an output.
Thank you.
Upvotes: 1
Views: 142
Reputation: 59579
Groupby and form lists and then create a dictionary
df.groupby('id')['value'].apply(list).to_dict()
# {1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'g', 'l']}
Upvotes: 5