DataFrame.to_dict(orient=’dict’)

>>> df = pd.DataFrame({'name':[1,2,3],"class":[11,22,33],"price":[111,222,333]})
>>> df
class name price
0 11 1 111
1 22 2 222
2 33 3 333

orient : str {‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, ‘index’}
Determines the type of the values of the dictionary.

dict (default) : dict like {column -> {index -> value}}

>>> df.to_dict(orient="dict")
{'class': {0: 11, 1: 22, 2: 33}, 'name': {0: 1, 1: 2, 2: 3}, 'price': {0: 111, 1: 222, 2: 333}}

list : dict like {column -> [values]}

>>> df.to_dict(orient="list")
{'class': [11, 22, 33], 'name': [1, 2, 3], 'price': [111, 222, 333]}

series : dict like {column -> Series(values)}

>>> df.to_dict(orient="series")
{'class': 0 11
1 22
2 33
Name: class, dtype: int64, 'name': 0 1
1 2
2 3
Name: name, dtype: int64, 'price': 0 111
1 222
2 333
Name: price, dtype: int64}

split : dict like {index -> [index], columns -> [columns], data -> [values]}

>>> df.to_dict(orient="split")
{'index': [0, 1, 2], 'columns': ['class', 'name', 'price'], 'data': [[11, 1, 111], [22, 2, 222], [33, 3, 333]]}

records : list like [{column -> value}, … , {column -> value}]

>>> df.to_dict(orient="records")
[{'class': 11, 'name': 1, 'price': 111}, {'class': 22, 'name': 2, 'price': 222}, {'class': 33, 'name': 3, 'price': 333}]

index : dict like {index -> {column -> value}}

>>> df.to_dict(orient="index")
{0: {'class': 11, 'name': 1, 'price': 111}, 1: {'class': 22, 'name': 2, 'price': 222}, 2: {'class': 33, 'name': 3, 'price': 333}}

DataFrame.to_dict(orient='dict')的更多相关文章

  1. pandas之Dataframe转成dict+过滤+index去重

    转成字典a = ['key1', 'key2', 'key3']b = ['1', '2', '3']data = pd.DataFrame(zip(a, b), columns=['project' ...

  2. Pandas v0.23.4手册汉化

    Pandas手册汉化 此页面概述了所有公共pandas对象,函数和方法.pandas.*命名空间中公开的所有类和函数都是公共的. 一些子包是公共的,其中包括pandas.errors, pandas. ...

  3. Python数据分析之Pandas操作大全

    从头到尾都是手码的,文中的所有示例也都是在Pycharm中运行过的,自己整理笔记的最大好处在于可以按照自己的思路来构建矿建,等到将来在需要的时候能够以最快的速度看懂并应用=_= 注:为方便表述,本章设 ...

  4. python之ETL数据清洗案例源代码

    #python语言 import pandas as pd import time data = pd.read_excel('ETL_数据清洗挑战.xlsx','测试数据',dtype=str)#读 ...

  5. 浅谈python之利用pandas和openpyxl读取excel数据

    在自学到接口自动化测试时, 发现要从excel中读取测试用例的数据, 假如我的数据是这样的: 最好是每行数据对应着一条测试用例, 为方便取值, 我选择使用pandas库, 先安装 pip instal ...

  6. Python数据分析(四)DataFrame, Series, ndarray, list, dict, tuple的相互转换

    转自:https://blog.csdn.net/lambsnow/article/details/78517340 import numpy as np import pandas as pd ## ...

  7. [译]使用to_dict将pandas.DataFrame转换为Python中的字典列表

    pandas.DataFrame.to_json返回的是JSON字符串,不是字典. 可以使用to_dict进行字典转换. 使用orient指定方向. >>> df col1 col2 ...

  8. 『Kaggle』分类任务_决策树&集成模型&DataFrame向量化操作

    决策树这节中涉及到了很多pandas中的新的函数用法等,所以我单拿出来详细的理解一下这些pandas处理过程,进一步理解pandas背后的数据处理的手段原理. 决策树程序 数据载入 pd.read_c ...

  9. pandas.to_json&to_dict&from_json&from_dict解读

    pandas 中的to_dict 可以对DataFrame类型的数据进行转换 可以选择六种的转换类型,分别对应于参数 ‘dict’, ‘list’, ‘series’, ‘split’, ‘recor ...

随机推荐

  1. Cqoi2017试题泛做

    Day1 4813: [Cqoi2017]小Q的棋盘 树形背包DP. #include <cstdio> #define maxn 110 #define R register #defi ...

  2. 【WC2016】论战捆竹竿

    已经快三周了啊--终于把挖的坑填了-- 首先显然是把除了自身的所有border拿出来,即做 \(\left\{ n - b_1, n - b_2, \dots, n - b_k, n \right\} ...

  3. 文章翻译:ABP如何在EF core中添加数据过滤器

    原文地址:https://aspnetboilerplate.com/Pages/Documents/Articles%5CHow-To%5Cadd-custom-data-filter-ef-cor ...

  4. Python字典里的5个黑魔法

    Python里面有3大数据结构:列表,字典和集合.字典是常用的数据结构,里面有一些重要的技巧用法,我把这些都整理到一起,熟练掌握这些技巧之后,对自己的功力大有帮助. 1.字典的排序: 用万金油sort ...

  5. Android重写HorizontalScrollView模仿ViewPager效果

    Android提供的ViewPager类太复杂,有时候没有必要使用,所以重写一个HorizontalScrollView来实现类似的效果,也可以当做Gallery来用 思路很简单,就是重写onTouc ...

  6. line-height与vertical-align:简单的属性不简单

    学习过CSS肯定对line-height与vertical-align两个属性有印象:line-height用来设置行高,vertical-align用来设置文本垂直方向的对齐方式,两种看似十分简单, ...

  7. 【转】diamond专题(四)—— 容灾机制

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  8. “fatal error: hdf5.h: 没有那个文件或目录”解决方法

    問題一: Installing Caffe without CUDA: fatal error: cublas_v2.h No such file: 在Makefile.config中修改,將CPU_ ...

  9. What is the !! (not not) operator in JavaScript?

    What is the !! (not not) operator in JavaScript? 解答1 Coerces强制 oObject to boolean. If it was falsey ...

  10. Use an Excel RTD Server with DCOM

    费好大劲找到的文章,留存. Use an Excel RTD Server with DCOM 如何使用DCOM的Excel RTD服务器 Microsoft Office Excel 2007,Mi ...