In [1]:
import pandas as pd
import pprint
from collections import OrderedDict

In [2]:
df = pd.DataFrame({'col1': [1, 2, 3], 'col2': ['a', 'x', 'あ']},
                  index=['row1', 'row2', 'row3'])

In [3]:
print(df)


      col1 col2
row1     1    a
row2     2    x
row3     3    あ

In [4]:
d = df.to_dict()

In [5]:
pprint.pprint(d)


{'col1': {'row1': 1, 'row2': 2, 'row3': 3},
 'col2': {'row1': 'a', 'row2': 'x', 'row3': 'あ'}}

In [6]:
print(type(d))


<class 'dict'>

In [7]:
d_dict = df.to_dict(orient='dict')

In [8]:
pprint.pprint(d_dict)


{'col1': {'row1': 1, 'row2': 2, 'row3': 3},
 'col2': {'row1': 'a', 'row2': 'x', 'row3': 'あ'}}

In [9]:
print(d_dict['col1'])


{'row1': 1, 'row2': 2, 'row3': 3}

In [10]:
print(type(d_dict['col1']))


<class 'dict'>

In [11]:
d_list = df.to_dict(orient='list')

In [12]:
pprint.pprint(d_list)


{'col1': [1, 2, 3], 'col2': ['a', 'x', 'あ']}

In [13]:
print(d_list['col1'])


[1, 2, 3]

In [14]:
print(type(d_list['col1']))


<class 'list'>

In [15]:
d_series = df.to_dict(orient='series')

In [16]:
pprint.pprint(d_series)


{'col1': row1    1
row2    2
row3    3
Name: col1, dtype: int64,
 'col2': row1    a
row2    x
row3    あ
Name: col2, dtype: object}

In [17]:
print(d_series['col1'])


row1    1
row2    2
row3    3
Name: col1, dtype: int64

In [18]:
print(type(d_series['col1']))


<class 'pandas.core.series.Series'>

In [19]:
d_split = df.to_dict(orient='split')

In [20]:
pprint.pprint(d_split)


{'columns': ['col1', 'col2'],
 'data': [[1, 'a'], [2, 'x'], [3, 'あ']],
 'index': ['row1', 'row2', 'row3']}

In [21]:
print(d_split['columns'])


['col1', 'col2']

In [22]:
print(type(d_split['columns']))


<class 'list'>

In [23]:
l_records = df.to_dict(orient='records')

In [24]:
pprint.pprint(l_records)


[{'col1': 1, 'col2': 'a'}, {'col1': 2, 'col2': 'x'}, {'col1': 3, 'col2': 'あ'}]

In [25]:
print(type(l_records))


<class 'list'>

In [26]:
print(l_records[0])


{'col1': 1, 'col2': 'a'}

In [27]:
print(type(l_records[0]))


<class 'dict'>

In [28]:
d_index = df.to_dict(orient='index')

In [29]:
pprint.pprint(d_index)


{'row1': {'col1': 1, 'col2': 'a'},
 'row2': {'col1': 2, 'col2': 'x'},
 'row3': {'col1': 3, 'col2': 'あ'}}

In [30]:
print(d_index['row1'])


{'col1': 1, 'col2': 'a'}

In [31]:
print(type(d_index['row1']))


<class 'dict'>

In [32]:
od = df.to_dict(into=OrderedDict)

In [33]:
pprint.pprint(od)


OrderedDict([('col1', OrderedDict([('row1', 1), ('row2', 2), ('row3', 3)])),
             ('col2',
              OrderedDict([('row1', 'a'), ('row2', 'x'), ('row3', 'あ')]))])

In [34]:
print(type(od))


<class 'collections.OrderedDict'>

In [35]:
print(od['col1'])


OrderedDict([('row1', 1), ('row2', 2), ('row3', 3)])

In [36]:
print(type(od['col1']))


<class 'collections.OrderedDict'>

In [37]:
print(df.index)


Index(['row1', 'row2', 'row3'], dtype='object')

In [38]:
print(df['col1'])


row1    1
row2    2
row3    3
Name: col1, dtype: int64

In [39]:
d_col = dict(zip(df.index, df['col1']))

In [40]:
print(d_col)


{'row1': 1, 'row2': 2, 'row3': 3}

In [41]:
d_row = dict(zip(df.columns, df.loc['row1']))

In [42]:
print(d_row)


{'col1': 1, 'col2': 'a'}