9951 explained code solutions for 126 technologies


python-pandasHow to convert dataframe to list of dicts


As opposed to converting single column values to list we can also convert full dataframe to list:

import pandas as pd

data = pd.DataFrame({
  'Vendor': ['US', 'US', 'US', 'KR', 'KR'],
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Phone Price': [204, 304, None, 405, None]
})

list = data.to_dict('records')ctrl + c
import pandas as pd

load Pandas module

pd.DataFrame

creates Pandas DataFrame object

.to_dict(

converts given dataframe to dict

'records'

will generate dict based on rows, not columns

list

final list of dicts


Usage example

import pandas as pd

data = pd.DataFrame({
  'Vendor': ['US', 'US', 'US', 'KR', 'KR'],
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Phone Price': [204, 304, None, 405, None]
})

list = data.to_dict('records')

print(list)
output
[{'Vendor': 'US', 'Phone': 'ip5', 'Phone Price': 204.0}, {'Vendor': 'US', 'Phone': 'ip6', 'Phone Price': 304.0}, {'Vendor': 'US', 'Phone': 'ip8', 'Phone Price': nan}, {'Vendor': 'KR', 'Phone': 'sms', 'Phone Price': 405.0}, {'Vendor': 'KR', 'Phone': 'xi', 'Phone Price': nan}]