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. kaliXSSbeef的使用

    Kali中Beef的安装和使用: 先打开终端输入 apt-get install beef-xss 然后切换到beef的安装目录 cd /usr/share/beef-xss 然后启动beef ./b ...

  2. 大哥带的XSS练习LEVE2

    0X01输出在html标签中的XSS 这里相当于我们把XSS代码插入到了 html中的<td>标签中 其他好看的 但是不是同源访问 <script> var body= doc ...

  3. spring-boot 中实现标准 redis 分布式锁

    一,前言 redis 现在已经成为系统缓存的必备组件,针对缓存读取更新操作,通常我们希望当缓存过期之后能够只有一个请求去更新缓存,其它请求依然使用旧的数据.这就需要用到锁,因为应用服务多数以集群方式部 ...

  4. 编译一个需要用特定key前面的应用程序

    LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # Build all java files in the java subdirectory L ...

  5. C++入门经典-例6.2-将二维数组进行行列对换

    1:一维数组的初始化有两种,一种是单个逐一赋值,一种是使用聚合方式赋值.聚合方式的例子如下: int a[3]={1,2,3}; int a[]={1,2,3};//编译器能够获得数组元素的个数 in ...

  6. Python--安装第三方库的方法

    一:安装方法(以numpy为例,安装其他包把numpy改为包名即可) 1.有设置python包中pip.exe环境变量的情况下 同时按住win+R输入cmd打开cmd,直接输入pip install ...

  7. yue

    1. 字节流与二进制文件 1.我的代码 public class Student { private int id; private String name; private int age; pri ...

  8. vue父子组件

    vue父子组件 新建 模板 小书匠  为什么要厘清哪个是父组件,哪个是子组件? 一开始浏览器接收和要显示的数据非常少,此时无需划分区域进行布局.随着页面数据量的增加,如果单纯一个窗口来加载和显示数据, ...

  9. adb自动化农药金币

    本贴仅为记录贴 记录adb 的环境配置及python脚本的交互 1.adb 的下载 通过搜索adb工具即可下载,这里提供一个共享地址https://pan.baidu.com/s/103ix26tZy ...

  10. Custom Configuration 的两种方法:1.Configuration Sections

    第一种Configuration Sections 1.App.config 2.CustomConfigurationManager.cs 3.TestProgram.cs. App.config ...