user4979733
user4979733

Reputation: 3411

Pandas: Convert dataframe to dict of lists

I have a dataframe like this:

col1, col2
A      0
A      1
B      2
C      3

I would like to get this:

{ A: [0,1], B: [2], C: [3] }

I tried:

df.set_index('col1')['col2'].to_dict()

but that is not quite correct. The first issue I have is 'A' is repeated, I end up getting A:1 only (0 gets overwritten). How to fix?

Upvotes: 8

Views: 1796

Answers (2)

piRSquared
piRSquared

Reputation: 294218

Solution

df.groupby('col1')['col2'].apply(lambda x: x.tolist()).to_dict()

{'A': [0, 1], 'B': [2], 'C': [3]}

Upvotes: 7

Alexander
Alexander

Reputation: 109526

You can use a dictionary comprehension on a groupby.

>>> {idx: group['col2'].tolist() 
     for idx, group in df.groupby('col1')}
{'A': [0, 1], 'B': [2], 'C': [3]}

Upvotes: 8

Related Questions