對pandas中to_dict的用法詳解
簡介:pandas 中的to_dict 可以對DataFrame類型的數(shù)據(jù)進行轉(zhuǎn)換
可以選擇六種的轉(zhuǎn)換類型,分別對應于參數(shù) ‘dict', ‘list', ‘series', ‘split', ‘records', ‘index',下面逐一介紹每種的用法
Help on method to_dict in module pandas.core.frame:
to_dict(orient='dict') method of pandas.core.frame.DataFrame instance
Convert DataFrame to dictionary.
Parameters
----------
orient : str {'dict', 'list', 'series', 'split', 'records', 'index'}
Determines the type of the values of the dictionary.
- dict (default) : dict like {column -> {index -> value}}
- list : dict like {column -> [values]}
- series : dict like {column -> Series(values)}
- split : dict like
{index -> [index], columns -> [columns], data -> [values]}
- records : list like
[{column -> value}, ... , {column -> value}]
- index : dict like {index -> {column -> value}}
.. versionadded:: 0.17.0
Abbreviations are allowed. `s` indicates `series` and `sp`
indicates `split`.
Returns
-------
result : dict like {column -> {index -> value}}
1、選擇參數(shù)orient='dict'
dict也是默認的參數(shù),下面的data數(shù)據(jù)類型為DataFrame結(jié)構(gòu), 會形成 {column -> {index -> value}}這樣的結(jié)構(gòu)的字典,可以看成是一種雙重字典結(jié)構(gòu)
- 單獨提取每列的值及其索引,然后組合成一個字典
- 再將上述的列屬性作為關(guān)鍵字(key),值(values)為上述的字典
查詢方式為 :data_dict[key1][key2]
- data_dict 為參數(shù)選擇orient='dict'時的數(shù)據(jù)名
- key1 為列屬性的鍵值(外層)
- key2 為內(nèi)層字典對應的鍵值
data
Out[9]:
pclass age embarked home.dest sex
1086 3rd 31.194181 UNKNOWN UNKNOWN male
12 1st 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 2nd 41.000000 Cherbourg New York, NY male
437 2nd 48.000000 Southampton Somerset / Bernardsville, NJ female
663 3rd 26.000000 Southampton UNKNOWN male
669 3rd 19.000000 Southampton England male
507 2nd 31.194181 Southampton Petworth, Sussex male
In[10]: data_dict=data.to_dict(orient= 'dict')
In[11]: data_dict
Out[11]:
{'age': {12: 31.19418104265403,
437: 48.0,
507: 31.19418104265403,
562: 41.0,
663: 26.0,
669: 19.0,
833: 32.0,
1036: 31.19418104265403,
1086: 31.19418104265403,
1108: 31.19418104265403},
'embarked': {12: 'Cherbourg',
437: 'Southampton',
507: 'Southampton',
562: 'Cherbourg',
663: 'Southampton',
669: 'Southampton',
833: 'Southampton',
1036: 'UNKNOWN',
1086: 'UNKNOWN',
1108: 'UNKNOWN'},
'home.dest': {12: 'Paris, France',
437: 'Somerset / Bernardsville, NJ',
507: 'Petworth, Sussex',
562: 'New York, NY',
663: 'UNKNOWN',
669: 'England',
833: 'Foresvik, Norway Portland, ND',
1036: 'UNKNOWN',
1086: 'UNKNOWN',
1108: 'UNKNOWN'},
'pclass': {12: '1st',
437: '2nd',
507: '2nd',
562: '2nd',
663: '3rd',
669: '3rd',
833: '3rd',
1036: '3rd',
1086: '3rd',
1108: '3rd'},
'sex': {12: 'female',
437: 'female',
507: 'male',
562: 'male',
663: 'male',
669: 'male',
833: 'male',
1036: 'male',
1086: 'male',
1108: 'male'}}
2、當關(guān)鍵字orient=' list' 時
和1中比較相似,只不過內(nèi)層變成了一個列表,結(jié)構(gòu)為{column -> [values]}
查詢方式為: data_list[keys][index]
data_list 為關(guān)鍵字orient='list' 時對應的數(shù)據(jù)名
keys 為列屬性的鍵值,如本例中的'age' , ‘embarked'等
index 為整型索引,從0開始到最后
In[19]: data_list=data.to_dict(orient='list')
In[20]: data_list
Out[20]:
{'age': [31.19418104265403,
31.19418104265403,
31.19418104265403,
32.0,
31.19418104265403,
41.0,
48.0,
26.0,
19.0,
31.19418104265403],
'embarked': ['UNKNOWN',
'Cherbourg',
'UNKNOWN',
'Southampton',
'UNKNOWN',
'Cherbourg',
'Southampton',
'Southampton',
'Southampton',
'Southampton'],
'home.dest': ['UNKNOWN',
'Paris, France',
'UNKNOWN',
'Foresvik, Norway Portland, ND',
'UNKNOWN',
'New York, NY',
'Somerset / Bernardsville, NJ',
'UNKNOWN',
'England',
'Petworth, Sussex'],
'pclass': ['3rd',
'1st',
'3rd',
'3rd',
'3rd',
'2nd',
'2nd',
'3rd',
'3rd',
'2nd'],
'sex': ['male',
'female',
'male',
'male',
'male',
'male',
'female',
'male',
'male',
'male']}
3、關(guān)鍵字參數(shù)orient='series'
形成結(jié)構(gòu){column -> Series(values)}
調(diào)用格式為:data_series[key1][key2]或data_dict[key1]
data_series 為數(shù)據(jù)對應的名字
key1 為列屬性的鍵值,如本例中的'age' , ‘embarked'等
key2 使用數(shù)據(jù)原始的索引(可選)
In[21]: data_series=data.to_dict(orient='series')
In[22]: data_series
Out[22]:
{'age': 1086 31.194181
12 31.194181
1036 31.194181
833 32.000000
1108 31.194181
562 41.000000
437 48.000000
663 26.000000
669 19.000000
507 31.194181
Name: age, dtype: float64, 'embarked': 1086 UNKNOWN
12 Cherbourg
1036 UNKNOWN
833 Southampton
1108 UNKNOWN
562 Cherbourg
437 Southampton
663 Southampton
669 Southampton
507 Southampton
Name: embarked, dtype: object, 'home.dest': 1086 UNKNOWN
12 Paris, France
1036 UNKNOWN
833 Foresvik, Norway Portland, ND
1108 UNKNOWN
562 New York, NY
437 Somerset / Bernardsville, NJ
663 UNKNOWN
669 England
507 Petworth, Sussex
Name: home.dest, dtype: object, 'pclass': 1086 3rd
12 1st
1036 3rd
833 3rd
1108 3rd
562 2nd
437 2nd
663 3rd
669 3rd
507 2nd
Name: pclass, dtype: object, 'sex': 1086 male
12 female
1036 male
833 male
1108 male
562 male
437 female
663 male
669 male
507 male
Name: sex, dtype: object}
4、關(guān)鍵字參數(shù)orient='split'
形成{index -> [index], columns -> [columns], data -> [values]}的結(jié)構(gòu),是將數(shù)據(jù)、索引、屬性名單獨脫離出來構(gòu)成字典
調(diào)用方式有 data_split[‘index'],data_split[‘data'],data_split[‘columns']
data_split=data.to_dict(orient='split')
data_split
Out[38]:
{'columns': ['pclass', 'age', 'embarked', 'home.dest', 'sex'],
'data': [['3rd', 31.19418104265403, 'UNKNOWN', 'UNKNOWN', 'male'],
['1st', 31.19418104265403, 'Cherbourg', 'Paris, France', 'female'],
['3rd', 31.19418104265403, 'UNKNOWN', 'UNKNOWN', 'male'],
['3rd', 32.0, 'Southampton', 'Foresvik, Norway Portland, ND', 'male'],
['3rd', 31.19418104265403, 'UNKNOWN', 'UNKNOWN', 'male'],
['2nd', 41.0, 'Cherbourg', 'New York, NY', 'male'],
['2nd', 48.0, 'Southampton', 'Somerset / Bernardsville, NJ', 'female'],
['3rd', 26.0, 'Southampton', 'UNKNOWN', 'male'],
['3rd', 19.0, 'Southampton', 'England', 'male'],
['2nd', 31.19418104265403, 'Southampton', 'Petworth, Sussex', 'male']],
'index': [1086, 12, 1036, 833, 1108, 562, 437, 663, 669, 507]}
5、當關(guān)鍵字orient='records' 時
形成[{column -> value}, … , {column -> value}]的結(jié)構(gòu)
整體構(gòu)成一個列表,內(nèi)層是將原始數(shù)據(jù)的每行提取出來形成字典
調(diào)用格式為data_records[index][key1]
data_records=data.to_dict(orient='records')
data_records
Out[41]:
[{'age': 31.19418104265403,
'embarked': 'UNKNOWN',
'home.dest': 'UNKNOWN',
'pclass': '3rd',
'sex': 'male'},
{'age': 31.19418104265403,
'embarked': 'Cherbourg',
'home.dest': 'Paris, France',
'pclass': '1st',
'sex': 'female'},
{'age': 31.19418104265403,
'embarked': 'UNKNOWN',
'home.dest': 'UNKNOWN',
'pclass': '3rd',
'sex': 'male'},
{'age': 32.0,
'embarked': 'Southampton',
'home.dest': 'Foresvik, Norway Portland, ND',
'pclass': '3rd',
'sex': 'male'},
{'age': 31.19418104265403,
'embarked': 'UNKNOWN',
'home.dest': 'UNKNOWN',
'pclass': '3rd',
'sex': 'male'},
{'age': 41.0,
'embarked': 'Cherbourg',
'home.dest': 'New York, NY',
'pclass': '2nd',
'sex': 'male'},
{'age': 48.0,
'embarked': 'Southampton',
'home.dest': 'Somerset / Bernardsville, NJ',
'pclass': '2nd',
'sex': 'female'},
{'age': 26.0,
'embarked': 'Southampton',
'home.dest': 'UNKNOWN',
'pclass': '3rd',
'sex': 'male'},
{'age': 19.0,
'embarked': 'Southampton',
'home.dest': 'England',
'pclass': '3rd',
'sex': 'male'},
{'age': 31.19418104265403,
'embarked': 'Southampton',
'home.dest': 'Petworth, Sussex',
'pclass': '2nd',
'sex': 'male'}]
6、當關(guān)鍵字orient='index' 時
形成{index -> {column -> value}}的結(jié)構(gòu),調(diào)用格式正好和'dict' 對應的反過來,請讀者自己思考
data_index=data.to_dict(orient='index')
data_index
Out[43]:
{12: {'age': 31.19418104265403,
'embarked': 'Cherbourg',
'home.dest': 'Paris, France',
'pclass': '1st',
'sex': 'female'},
437: {'age': 48.0,
'embarked': 'Southampton',
'home.dest': 'Somerset / Bernardsville, NJ',
'pclass': '2nd',
'sex': 'female'},
507: {'age': 31.19418104265403,
'embarked': 'Southampton',
'home.dest': 'Petworth, Sussex',
'pclass': '2nd',
'sex': 'male'},
562: {'age': 41.0,
'embarked': 'Cherbourg',
'home.dest': 'New York, NY',
'pclass': '2nd',
'sex': 'male'},
663: {'age': 26.0,
'embarked': 'Southampton',
'home.dest': 'UNKNOWN',
'pclass': '3rd',
'sex': 'male'},
669: {'age': 19.0,
'embarked': 'Southampton',
'home.dest': 'England',
'pclass': '3rd',
'sex': 'male'},
833: {'age': 32.0,
'embarked': 'Southampton',
'home.dest': 'Foresvik, Norway Portland, ND',
'pclass': '3rd',
'sex': 'male'},
1036: {'age': 31.19418104265403,
'embarked': 'UNKNOWN',
'home.dest': 'UNKNOWN',
'pclass': '3rd',
'sex': 'male'},
1086: {'age': 31.19418104265403,
'embarked': 'UNKNOWN',
'home.dest': 'UNKNOWN',
'pclass': '3rd',
'sex': 'male'},
1108: {'age': 31.19418104265403,
'embarked': 'UNKNOWN',
'home.dest': 'UNKNOWN',
'pclass': '3rd',
'sex': 'male'}}
以上這篇對pandas中to_dict的用法詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python Manager 之dict KeyError問題的解決
今天小編就為大家分享一篇python Manager 之dict KeyError問題的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
python實現(xiàn)對服務(wù)器腳本敏感信息的加密解密功能
這篇文章主要介紹了python實現(xiàn)對服務(wù)器腳本敏感信息的加密解密功能,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08
python web.py開發(fā)httpserver解決跨域問題實例解析
這篇文章主要介紹了python web.py開發(fā)httpserver解決跨域問題實例解析,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02
python3定位并識別圖片驗證碼實現(xiàn)自動登錄功能
這篇文章主要介紹了python3定位并識別圖片驗證碼實現(xiàn)自動登錄功能,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01

