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. 小程序上传wx.uploadFile - 小程序请假-请求

    小程序上传wx.uploadFile UploadTask wx.uploadFile(Object object) 将本地资源上传到服务器.客户端发起一个 HTTPS POST 请求,其中 cont ...

  2. sql in条件 超过1000字符的处理方法

    private string getOracleSQLIn(string[] ids, string field) { int count = Math.Min(ids.Length, 1000); ...

  3. Selenium 加载Chrome/Firefox浏览器配置文件

    Selenium启动浏览器时,默认是打开一个新用户,不会加载原有的配置以及插件.但有些时候我们可能需要加载默认配置. 一.Chrome浏览器 1.在Chrome浏览器的地址栏输入:chrome://v ...

  4. Unity3D_(API)射线检测Raycast()

    Unity射线检测官方文档: 传送门 一.检测前方是否有游戏物体(射线无限长度) 二.检测前方是否有游戏物体(射线长度为1m) 三.检测前方游戏物体碰撞信息(射线无限长度): 四.指定检测碰撞Tag层 ...

  5. input(移动端iOS)输入内容时调用软件盘后页面底部留白问题

    iOS/input输入框调用软键盘底部留白 只需input输入框失去焦点时,让页面自动下移即可恢复 <input placeholder="请输入用户名" v-model=& ...

  6. 【CentOS】yum安装教训

    前言:本来想安装sl在新安装的centos7上,网上搜了教程,很多都是先要你yum -y update,如下: 1.更新yum源: yum -y update 2.依赖安装: wget http:// ...

  7. Orchestrator MySQL主库故障切换的几种类型

    Orchestrator 对MySQL主库的故障切换分为自动切换和手动切换. 手动切换又分为recover.force-master-failover.force-master-takeover以及g ...

  8. Fragment 的 replace 和 add 方法的区别?

    Fragment 本身并没有 replace 和 add 方法,这里的理解应该为使用 FragmentManager 的 replace 和 add 两种方法切换 Fragment 时有什么不同.我们 ...

  9. apache访问日志

    #错误日志ErrorLog "logs/dummy-host2.example.com-error_log" #访问日志CustomLog "logs/dummy-hos ...

  10. PHP加速器eAccelerator安装

    程序说明 eAccelerator是一个自由开放源码php加速器,优化和动态内容缓存,提高了php脚本的缓存性能,使得PHP脚本在编译的状态下,对 服务器的开销几乎为零. 它还有对脚本起优化作用,以加 ...