dataframe转字典

一,DataFrame转字典格式

复制代码
  • 1
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值},....};

复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
>>> 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

复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
>>> 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为各列对应值的列表

复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
>>> 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 对应值得列表:

复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
>>> df col_1 col_2 row1 1 0.50 row2 2 0.75 >>> df.to_dict('list')['col_2'] [0.5, 0.75]

'series'

复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
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统一都是列表形式;

复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
>>> 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数据表 中全部 列名或者行名 的列表形式,例如我想得到全部列名:

复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
>>> 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 列表形式,由全部列名与每一行的值形成一一对应的映射关系:

复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
>>> 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}得数据:

复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
>>> 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相似):

复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
>>> 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转列表格式

转成列表不常用到

形式为,结果是列表套列表的格式,元素是由每一行的所有信息组成的列表,

复制代码
  • 1
df1 = df.values.tolist()

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

posted @   Franciszw  阅读(2025)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开