user4979733 user4979733 - 1 year ago 245
Python Question

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:


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?

Answer Source

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]}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download