'Pandas: Convert dataframe columns into dict with col title as dict key and col values as dict values

I have a data frame that looks like:

      NAME              ID 
155  ARBITRARY_A    697381   
208  ARBITRARY_B    691820   
265  ARBITRARY_C    691782   
272  ARBITRARY_D    695593 

I want to convert it into a list of dictionaries that looks like:

[{name:ARBITRARY_A, id:697381}, {name:ARBITRARY_B, id:691820},
 {name:ARBITRARY_C, id:691782}, {name:ARBITRARY_D, id:695593}]

What is the fastest/optimum way to do this operation?



Solution 1:[1]

As indicated in comments, pandas.DataFrame.to_dict() can be used. And in your case you need to orient as record:

Code:

df.to_dict('record')

Test Code:

df = pd.read_fwf(StringIO(u"""
          NAME              ID
    155  ARBITRARY_A    697381
    208  ARBITRARY_B    691820
    265  ARBITRARY_C    691782
    272  ARBITRARY_D    695593"""),
                 header=1, index_col=0)

print(df)
print(df.to_dict('record'))

Results:

            NAME      ID
155  ARBITRARY_A  697381
208  ARBITRARY_B  691820
265  ARBITRARY_C  691782
272  ARBITRARY_D  695593

[{u'NAME': u'ARBITRARY_A', u'ID': 697381L}, {u'NAME': u'ARBITRARY_B', u'ID': 691820L}, {u'NAME': u'ARBITRARY_C', u'ID': 691782L}, {u'NAME': u'ARBITRARY_D', u'ID': 695593L}]

Solution 2:[2]

You can squeeze a little more performance out of it by doing the comprehensions yourself

v = df.values.tolist()
c = df.columns.values.tolist()

[dict(zip(c, x)) for x in v]

[{'ID': 697381L, 'NAME': 'ARBITRARY_A'},
 {'ID': 691820L, 'NAME': 'ARBITRARY_B'},
 {'ID': 691782L, 'NAME': 'ARBITRARY_C'},
 {'ID': 695593L, 'NAME': 'ARBITRARY_D'}]

small given df
enter image description here

larger d1
enter image description here

Solution 3:[3]

Try:

df.to_dict()

if this does not produce the intended result try to transpose the DataFrame:

df.T.to_dict()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stephen Rauch
Solution 2 piRSquared
Solution 3 Philipp Schwarz