Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Dataframe to a Dictionary with List Values

Suppose I have a Dataframe df :

Label1    Label2        Label3
key1      col1value1    col2value1
key2      col1value2    col2value2
key3      col1value3    col2value3


dict1 = df.set_index('Label1').to_dict() 

This works when we have 2 columns..

Expected Output:

my_dict = {key1: [col1value1,col2value1] , key2: [ col1value2,col2value2] , key3:[col1value3,col2value3] }

Can I use to_dict on Dataframe df to have a key with 2 other columns as values in form of list ??

like image 685
Hypothetical Ninja Avatar asked May 07 '14 04:05

Hypothetical Ninja


People also ask

How do you convert a Dataframe to a dictionary?

Pandas is one of those packages and makes importing and analyzing data much easier. Pandas .to_dict() method is used to convert a dataframe into a dictionary of series or list like data type depending on orient parameter. Syntax: DataFrame.to_dict(orient=’dict’, into=)

How to convert a Dataframe to a list of values?

It is a case when we have DataFrame, which needs to be converted into the dictionary object such that column label should be the keys in the dictionary, and all the columns’ data should be added into the resultant dict as a list of values against each key. In that case, we can use 'list' parameter of the DataFrame.to_dict () function.

How to create a Python Dict object from a Dataframe?

After analyzing the data, we need to convert the resultant DataFrame back to its original format like CSV files or a dictionary. Or sometimes, we need to convert it into some other form. Pandas have a DataFrame.to_dict () function to create a Python dict object from DataFrame. into: It is used to define the type of resultant dict.

How do I convert pandas to a dictionary?

Pandas .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

Well you could use a dictionary comprehension and iterrows:

print {key:row.tolist() for key,row in df.set_index('Label1').iterrows()}

{'key3': ['col1value3', 'col2value3'],
 'key2': ['col1value2', 'col2value2'], 
 'key1': ['col1value1', 'col2value1']}

Also, I think the following will work too:

df = df.set_index('Label1')
print df.T.to_dict(outtype='list')

{'key3': ['col1value3', 'col2value3'],
 'key2': ['col1value2', 'col2value2'],
 'key1': ['col1value1', 'col2value1']}

Update as of fall 2017; outtype is no longer the keyword argument. Use orient instead:

In [11]: df.T.to_dict(orient='list')
Out[11]: 
{'key1': ['col1value1', 'col2value1'],
 'key2': ['col1value2', 'col2value2'],
 'key3': ['col1value3', 'col2value3']}
like image 193
Karl D. Avatar answered Oct 05 '22 05:10

Karl D.