Convert pandas DataFrame to dictionary

Pandas DataFrame can be converted to dictionary using to_dict method.

import pandas

a = [
    ('Texas', 29),
    ('New York', 19),
    ('California', 40),
    ('Florida', 21),
]

df = pandas.DataFrame(a, columns=['State', 'Population'])

d = df.to_dict()
d_dict = df.to_dict(orient='dict')

print(d)
# {'State': {0: 'Texas', 1: 'New York', 2: 'California', 3: 'Florida'}, 'Population': {0: 29, 1: 19, 2: 40, 3: 21}}

print(d_dict)
# {'State': {0: 'Texas', 1: 'New York', 2: 'California', 3: 'Florida'}, 'Population': {0: 29, 1: 19, 2: 40, 3: 21}}

to_dict takes the option argument "orient" and no argument means it is dict. If orient argument is dict, the output dictionary has column-based keys.

Orient options

orient option can be dict, list, series, split, records, index. dict is default.

import pandas

a = [
    ('Texas', 29),
    ('New York', 19),
    ('California', 40),
    ('Florida', 21),
]

df = pandas.DataFrame(a, columns=['State', 'Population'])

d = df.to_dict()
d_dict = df.to_dict(orient='dict')
d_list = df.to_dict(orient='list')
d_series = df.to_dict(orient='series')
d_split = df.to_dict(orient='split')
d_records = df.to_dict(orient='records')
d_index = df.to_dict(orient='index')

print(d)
# {'State': {0: 'Texas', 1: 'New York', 2: 'California', 3: 'Florida'}, 'Population': {0: 29, 1: 19, 2: 40, 3: 21}}

print(d_dict)
# {'State': {0: 'Texas', 1: 'New York', 2: 'California', 3: 'Florida'}, 'Population': {0: 29, 1: 19, 2: 40, 3: 21}}

print(d_list)
# {'State': ['Texas', 'New York', 'California', 'Florida'], 'Population': [29, 19, 40, 21]}

print(d_series)
# {'State': 0         Texas
# 1      New York
# 2    California
# 3       Florida
# Name: State, dtype: object, 'Population': 0    29
# 1    19
# 2    40
# 3    21
# Name: Population, dtype: int64}

print(d_split)
# {'index': [0, 1, 2, 3], 'columns': ['State', 'Population'], 'data': [['Texas', 29], ['New York', 19], ['California', 40], ['Florida', 21]]}

print(d_records)
# [{'State': 'Texas', 'Population': 29}, {'State': 'New York', 'Population': 19}, {'State': 'California', 'Population': 40}, {'State': 'Florida', 'Population': 21}]

print(d_index)
# {0: {'State': 'Texas', 'Population': 29}, 1: {'State': 'New York', 'Population': 19}, 2: {'State': 'California', 'Population': 40}, 3: {'State': 'Florida', 'Population': 21}}

pandas DataFrame

pandas tutorial