Pandas 基本使用(三) — DataFrame.to_dict() 函數使用!

Pandas 處理數據的基本類型爲 DataFrame,數據清洗時不可必然會關係到數據類型轉化問題,Pandas 在這方面也做的也非常不錯,其中經常用的是 DataFrame.to_dict() 函數之間轉化爲字典類型;除了轉化爲字典之外,Pandas 還提供向 jsonhtmllatexcsv等格式的轉換:

Snipaste_2020-03-08_23-57-17.jpg

to_dict() 函數基本語法

DataFrame.to_dict (self, orient='dict’, into=) — 官方文檔

函數種只需要填寫一個參數:orient 即可 ,但對於寫入orient的不同,字典的構造方式也不同,官網一共給出了6種,並且其中一種是列表類型:

  • orient =‘dict’,是函數默認的,轉化後的字典形式:{column(列名) : {index(行名) : value(值) )}};
  • orient =‘list’ ,轉化後的字典形式:{column(列名) :{[ values ](值)}};
  • orient =‘series’ ,轉化後的字典形式:{column(列名) : Series (values) (值)};
  • orient =‘split’ ,轉化後的字典形式:{‘index’ : [index],‘columns’ :[columns],’data‘ : [values]};
  • orient =‘records’ ,轉化後是 list形式:[{column(列名) : value(值)}…{column:value}];
  • orient =‘index’ ,轉化後的字典形式:{index(值) : {column(列名) : value(值)}};

備註:

1,上面中 value 代表數據表中的值,column表示列名,index 表示行名,如下圖所示:

Snipaste_2020-03-09_00-16-58.jpg

2,{ }表示字典數據類型,字典中的數據是以 {key : value} 的形式顯示,是鍵名和鍵值一一對應形成的。

2,關於6種構造方式進行代碼實例

六種構造方式所處理 DataFrame 數據是統一的,如下:

>>> import pandas as pd
>>> df =pd.DataFrame({'col_1':[1,2],'col_2':[0.5,0.75]},index =['row1','row2'])
>>> df
      col_1  col_2
row1      1   0.50
row2      2   0.75
2.1,orient =‘dict’ — {column(列名) : {index(行名) : value(值) )}}

to_dict('list') 時,構造好的字典形式:{第一列的列名:{第一行的行名:value值,第二行行名,value值},…};

>>> df
      col_1  col_2
row1      1   0.50
row2      2   0.75
>>> df.to_dict('dict')
{'col_1': {'row1': 1, 'row2': 2}, 'col_2': {'row1': 0.5, 'row2': 0.75}}

orient = 'dict 可以很方面得到 在某一列對應的行名與各值之間的字典數據類型,例如在源數據上面我想得到在col_1這一列行名與各值之間的字典,直接在生成字典查詢列名爲col_1

>>> df
      col_1  col_2
row1      1   0.50
row2      2   0.75
>>> df.to_dict('dict')['col_1']
{'row1': 1, 'row2': 2}
2.2,orient =‘list’ — {column(列名) :{[ values ](值)}};

生成字典中 key爲各列名,value爲各列對應值的列表

>>> df
      col_1  col_2
row1      1   0.50
row2      2   0.75
>>> df.to_dict('list')
{'col_1': [1, 2], 'col_2': [0.5, 0.75]}

orient = 'list' 時,可以很方面得到 在某一列 各值所生成的列表集合,例如我想得到col_2 對應值得列表:

>>> df
      col_1  col_2
row1      1   0.50
row2      2   0.75
>>> df.to_dict('list')['col_2']
[0.5, 0.75]
2.3,orient =‘series’ — {column(列名) : Series (values) (值)};

orient ='series'orient = 'list' 唯一區別就是,這裏的 valueSeries數據類型,而前者爲列表類型

>>> df
      col_1  col_2
row1      1   0.50
row2      2   0.75
>>> df.to_dict('series')
{'col_1': row1    1
row2    2
Name: col_1, dtype: int64, 'col_2': row1    0.50
row2    0.75
Name: col_2, dtype: float64}
2.4,orient =‘split’ — {‘index’ : [index],‘columns’ :[columns],’data‘ : [values]};

orient ='split' 得到三個鍵值對,列名、行名、值各一個,value統一都是列表形式;

>>> df
      col_1  col_2
row1      1   0.50
row2      2   0.75
>>> df.to_dict('split')
{'index': ['row1', 'row2'], 'columns': ['col_1', 'col_2'], 'data': [[1, 0.5], [2, 0.75]]}

orient = 'split' 可以很方面得到 DataFrame數據表 中全部 列名或者行名 的列表形式,例如我想得到全部列名:

>>> df
      col_1  col_2
row1      1   0.50
row2      2   0.75
>>> df.to_dict('split')['columns']
['col_1', 'col_2']
2.5,orient =‘records’ — [{column:value(值)},{column:value}…{column:value}];

注意的是,orient ='records' 返回的數據類型不是 dict ; 而是list 列表形式,由全部列名與每一行的值形成一一對應的映射關係:

>>> df
      col_1  col_2
row1      1   0.50
row2      2   0.75
>>> df.to_dict('records')
[{'col_1': 1, 'col_2': 0.5}, {'col_1': 2, 'col_2': 0.75}]

這個構造方式的好處就是,很容易得到 列名與某一行值形成得字典數據;例如我想要第2行{column:value}得數據:

>>> df
      col_1  col_2
row1      1   0.50
row2      2   0.75
>>> df.to_dict('records')[1]
{'col_1': 2, 'col_2': 0.75}
2.6,orient =‘index’ — {index:{culumn:value}};

orient ='index'2.1用法剛好相反,求某一行中列名與值之間一一對應關係(查詢效果與2.5相似):

>>> df
      col_1  col_2
row1      1   0.50
row2      2   0.75
>>> df.to_dict('index')
{'row1': {'col_1': 1, 'col_2': 0.5}, 'row2': {'col_1': 2, 'col_2': 0.75}}

#查詢行名爲 row2 列名與值一一對應字典數據類型
>>> df.to_dict('index')['row2']
{'col_1': 2, 'col_2': 0.75}

3,to_dict() 函數其它用法

在部分 2 只展示了這個函數的一部分功能,還有其他用法,例如可以快速索引第幾行第幾列的值(pandas內置索引函數),例如我想要第二行第二列的值,可以這樣操作:

#to_dict 版本的:
>>> df
      col_1  col_2
row1      1   0.50
row2      2   0.75
>>> dict_1 = df.to_dict('index')
>>> dict_2 = dict_1[list(dict_1.keys()[1])]
>>> dict_2[list(dict_2.keys())[1]]
0.75


# pandas自帶的
>>> df
      col_1  col_2
row1      1   0.50
row2      2   0.75
>>> df.iloc[1,1]
0.75

相對會麻煩點,但重要的是整個實現過程;最後,感謝閱讀!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章