pandas 中的to_dict 可以对DataFrame类型的数据进行转换
可以选择六种的转换类型,分别对应于参数 ‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, ‘index’,下面逐一介绍每种的用法
0、原始数据
import pandas as pd
data = pd.DataFrame([['3rd', 31.194181, 'UNKNOWN', 'UNKNOWN', 'male'],
['1rd', 31.194181, 'Cherbourg', 'Paris, France', 'female'],
['3rd', 31.194181, 'UNKNOWN', 'UNKNOWN', 'male'],
['3rd', 32.000000, 'Southampton', 'Foresvik, Norway Portland, ND', 'male'],
['3rd', 31.194181, 'UNKNOWN', 'UNKNOWN', 'male'],
['2rd', 41.000000, 'Cherbourg', 'New York, NY', 'male'],
['2rd', 48.000000, 'Southampton', 'Somerset / Bernardsville, NJ', 'female'],
['3rd', 26.000000, 'Southampton', 'UNKNOWN', 'male'],
['3rd', 19.000000, 'Southampton', 'England', 'male'],
['2rd', 31.194181, 'Southampton', 'Petworth, Sussex', 'male']],
index=['1086', '12', '1036', '833', '1108', '562', '437', '663', '669', '507'],
columns=['pclass', 'age', 'embarked', 'home.dest', 'sex'])
print(data)
输出结果:
pclass age embarked home.dest sex
1086 3rd 31.194181 UNKNOWN UNKNOWN male
12 1rd 31.194181 Cherbourg Paris, France female
1036 3rd 31.194181 UNKNOWN UNKNOWN male
833 3rd 32.000000 Southampton Foresvik, Norway Portland, ND male
1108 3rd 31.194181 UNKNOWN UNKNOWN male
562 2rd 41.000000 Cherbourg New York, NY male
437 2rd 48.000000 Southampton Somerset / Bernardsville, NJ female
663 3rd 26.000000 Southampton UNKNOWN male
669 3rd 19.000000 Southampton England male
507 2rd 31.194181 Southampton Petworth, Sussex male
1、选择参数orient=’dict’ ,可以看成是一种双重字典结构(columns是外层键,index是内层键)
形成结构{column -> {index -> value}}
data_dict = data.to_dict(orient='dict')
print(data_dict)
输出结果为:
{
'pclass': {
'1086': '3rd',
'12': '1rd',
'1036': '3rd',
'833': '3rd',
'1108': '3rd',
'562': '2rd',
'437': '2rd',
'663': '3rd',
'669': '3rd',
'507': '2rd'
},
'age': {
'1086': 31.194181,