dataframe轉字典


一,DataFrame轉字典格式

DataFrame.to_dict (orient='dict')

函數種只需要填寫一個參數:orient 即可 ,但對於寫入orient的不同,字典的構造方式也不同,官網一共給出了6種,orient的名字與轉成字典value的格式有關系.

  • 1.orient ='dict',是函數默認的,轉化后的字典形式,字典的value是一個字典,字典的kv分別是該行名和該列對應的值

    {column(列名) : {index(行名) : value(值) )}};

  • 2.orient ='list' ,轉化后的字典形式,字典的value是一個列表,該列表是把這一列的值全部取出來

    {column(列名) :{ values }};

  • 3.orient ='series' ,轉化后的字典形式,value和列表類似是一個Series格式的

    {column(列名) : Series (values) (值)};

  • 4.orient ='split' ,轉化后的字典形式,把df格式拆分成3塊,行,列,和值

    {'index' : [index],‘columns' :[columns],’data‘ : [values]};

  • 5.orient ='records' ,轉化后是 list形式由字典組成,每個字典都是一行,k是對應列名,v是對應值:

    [{column(列名) : value(值)}......{column:value}];

  • 6.orient ='index' ,轉化后的字典形式:按一行一行展開,每個元素又是字典,k是列名,v是對應值

    {index(值) : {column(列名) : value(值)}};

'dict'

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}

'list'

生成字典中 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]

'series'

orient ='series'` 與 `orient = 'list'` 唯一區別就是,這里的 `value` 是 `Series數據類型`,而前者為`列表類型
>>> 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}

'split'

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']

'records'

注意的是,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}

'index'

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}

二,DataFrame轉列表格式

轉成列表不常用到

形式為,結果是列表套列表的格式,元素是由每一行的所有信息組成的列表,

df1 = df.values.tolist()

這里的tolist()是array的方法,不是pandas的方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM