Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 308
user4979733 Avatar asked May 11 '16 01:05

user4979733


People also ask

How do I convert a Pandas DataFrame to a dictionary?

To convert pandas DataFrame to Dictionary object, use to_dict() method, this takes orient as dict by default which returns the DataFrame in format {column -> {index -> value}} . When no orient is specified, to_dict() returns in this format.

When we create DataFrame from list of dictionaries to dictionary keys will become?

Example 1: As we know while creating a data frame from the dictionary, the keys will be the columns in the resulted Dataframe. When we create Dataframe from a list of dictionaries, matching keys will be the columns and corresponding values will be the rows of the Dataframe.

What does to_dict do in Python?

to_dict() method is used to convert a dataframe into a dictionary of series or list like data type depending on orient parameter. Parameters: orient: String value, ('dict', 'list', 'series', 'split', 'records', 'index') Defines which dtype to convert Columns(series into).


1 Answers

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]}
like image 178
Alexander Avatar answered Nov 12 '22 12:11

Alexander