怎么在pandas中使用to_dict方法?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
十多年的崇禮網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整崇禮建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“崇禮網(wǎng)站設(shè)計”,“崇禮網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實(shí)執(zhí)行。簡介:pandas 中的to_dict 可以對DataFrame類型的數(shù)據(jù)進(jìn)行轉(zhuǎn)換
可以選擇六種的轉(zhuǎn)換類型,分別對應(yīng)于參數(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也是默認(rèn)的參數(shù),下面的data數(shù)據(jù)類型為DataFrame結(jié)構(gòu), 會形成 {column -> {index -> value}}這樣的結(jié)構(gòu)的字典,可以看成是一種雙重字典結(jié)構(gòu)
- 單獨(dú)提取每列的值及其索引,然后組合成一個字典
- 再將上述的列屬性作為關(guān)鍵字(key),值(values)為上述的字典
查詢方式為 :data_dict[key1][key2]
- data_dict 為參數(shù)選擇orient='dict'時的數(shù)據(jù)名
- key1 為列屬性的鍵值(外層)
- key2 為內(nèi)層字典對應(yīng)的鍵值
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、當(dāng)關(guān)鍵字orient=' list' 時
和1中比較相似,只不過內(nèi)層變成了一個列表,結(jié)構(gòu)為{column -> [values]}
查詢方式為: data_list[keys][index]
data_list 為關(guān)鍵字orient='list' 時對應(yīng)的數(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ù)對應(yīng)的名字
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ù)、索引、屬性名單獨(dú)脫離出來構(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、當(dāng)關(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、當(dāng)關(guān)鍵字orient='index' 時
形成{index -> {column -> value}}的結(jié)構(gòu),調(diào)用格式正好和'dict' 對應(yīng)的反過來,請讀者自己思考
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'}}
關(guān)于怎么在pandas中使用to_dict方法問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。