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)
In [4]:
d = df.to_dict()
In [5]:
pprint.pprint(d)
In [6]:
print(type(d))
In [7]:
d_dict = df.to_dict(orient='dict')
In [8]:
pprint.pprint(d_dict)
In [9]:
print(d_dict['col1'])
In [10]:
print(type(d_dict['col1']))
In [11]:
d_list = df.to_dict(orient='list')
In [12]:
pprint.pprint(d_list)
In [13]:
print(d_list['col1'])
In [14]:
print(type(d_list['col1']))
In [15]:
d_series = df.to_dict(orient='series')
In [16]:
pprint.pprint(d_series)
In [17]:
print(d_series['col1'])
In [18]:
print(type(d_series['col1']))
In [19]:
d_split = df.to_dict(orient='split')
In [20]:
pprint.pprint(d_split)
In [21]:
print(d_split['columns'])
In [22]:
print(type(d_split['columns']))
In [23]:
l_records = df.to_dict(orient='records')
In [24]:
pprint.pprint(l_records)
In [25]:
print(type(l_records))
In [26]:
print(l_records[0])
In [27]:
print(type(l_records[0]))
In [28]:
d_index = df.to_dict(orient='index')
In [29]:
pprint.pprint(d_index)
In [30]:
print(d_index['row1'])
In [31]:
print(type(d_index['row1']))
In [32]:
od = df.to_dict(into=OrderedDict)
In [33]:
pprint.pprint(od)
In [34]:
print(type(od))
In [35]:
print(od['col1'])
In [36]:
print(type(od['col1']))
In [37]:
print(df.index)
In [38]:
print(df['col1'])
In [39]:
d_col = dict(zip(df.index, df['col1']))
In [40]:
print(d_col)
In [41]:
d_row = dict(zip(df.columns, df.loc['row1']))
In [42]:
print(d_row)