溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

怎么在pandas中使用to_dict方法

發(fā)布時(shí)間:2021-01-06 15:54:56 來(lái)源:億速云 閱讀:372 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

怎么在pandas中使用to_dict方法?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

簡(jiǎn)介:pandas 中的to_dict 可以對(duì)DataFrame類(lèi)型的數(shù)據(jù)進(jìn)行轉(zhuǎn)換

可以選擇六種的轉(zhuǎn)換類(lèi)型,分別對(duì)應(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ù)類(lèi)型為DataFrame結(jié)構(gòu), 會(huì)形成 {column -> {index -> value}}這樣的結(jié)構(gòu)的字典,可以看成是一種雙重字典結(jié)構(gòu)

- 單獨(dú)提取每列的值及其索引,然后組合成一個(gè)字典

- 再將上述的列屬性作為關(guān)鍵字(key),值(values)為上述的字典

查詢(xún)方式為 :data_dict[key1][key2]

- data_dict 為參數(shù)選擇orient='dict'時(shí)的數(shù)據(jù)名

- key1 為列屬性的鍵值(外層)

- key2 為內(nèi)層字典對(duì)應(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' 時(shí)

和1中比較相似,只不過(guò)內(nèi)層變成了一個(gè)列表,結(jié)構(gòu)為{column -> [values]}

查詢(xún)方式為: data_list[keys][index]

data_list 為關(guān)鍵字orient='list' 時(shí)對(duì)應(yīng)的數(shù)據(jù)名

keys 為列屬性的鍵值,如本例中的'age' , ‘embarked'等

index 為整型索引,從0開(kāi)始到最后

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ù)對(duì)應(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ú)脫離出來(lái)構(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' 時(shí)

形成[{column -> value}, … , {column -> value}]的結(jié)構(gòu)

整體構(gòu)成一個(gè)列表,內(nèi)層是將原始數(shù)據(jù)的每行提取出來(lái)形成字典

調(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' 時(shí)

形成{index -> {column -> value}}的結(jié)構(gòu),調(diào)用格式正好和'dict' 對(duì)應(yīng)的反過(guò)來(lái),請(qǐ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方法問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI