DataFrame.to_dict(orient='dict')
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')的更多相关文章
- pandas之Dataframe转成dict+过滤+index去重
转成字典a = ['key1', 'key2', 'key3']b = ['1', '2', '3']data = pd.DataFrame(zip(a, b), columns=['project' ...
- Pandas v0.23.4手册汉化
Pandas手册汉化 此页面概述了所有公共pandas对象,函数和方法.pandas.*命名空间中公开的所有类和函数都是公共的. 一些子包是公共的,其中包括pandas.errors, pandas. ...
- Python数据分析之Pandas操作大全
从头到尾都是手码的,文中的所有示例也都是在Pycharm中运行过的,自己整理笔记的最大好处在于可以按照自己的思路来构建矿建,等到将来在需要的时候能够以最快的速度看懂并应用=_= 注:为方便表述,本章设 ...
- python之ETL数据清洗案例源代码
#python语言 import pandas as pd import time data = pd.read_excel('ETL_数据清洗挑战.xlsx','测试数据',dtype=str)#读 ...
- 浅谈python之利用pandas和openpyxl读取excel数据
在自学到接口自动化测试时, 发现要从excel中读取测试用例的数据, 假如我的数据是这样的: 最好是每行数据对应着一条测试用例, 为方便取值, 我选择使用pandas库, 先安装 pip instal ...
- Python数据分析(四)DataFrame, Series, ndarray, list, dict, tuple的相互转换
转自:https://blog.csdn.net/lambsnow/article/details/78517340 import numpy as np import pandas as pd ## ...
- [译]使用to_dict将pandas.DataFrame转换为Python中的字典列表
pandas.DataFrame.to_json返回的是JSON字符串,不是字典. 可以使用to_dict进行字典转换. 使用orient指定方向. >>> df col1 col2 ...
- 『Kaggle』分类任务_决策树&集成模型&DataFrame向量化操作
决策树这节中涉及到了很多pandas中的新的函数用法等,所以我单拿出来详细的理解一下这些pandas处理过程,进一步理解pandas背后的数据处理的手段原理. 决策树程序 数据载入 pd.read_c ...
- pandas.to_json&to_dict&from_json&from_dict解读
pandas 中的to_dict 可以对DataFrame类型的数据进行转换 可以选择六种的转换类型,分别对应于参数 ‘dict’, ‘list’, ‘series’, ‘split’, ‘recor ...
随机推荐
- Mysql 执行安装脚本报错Changed limits:
安装Mysql软件的时候报错,如下: [root@db bin]# ./mysql_install_db --basedir=/usr/local/mysql --datadir=/u01/app/m ...
- python学习之路(3)
字符串和编码 因为python最早只支持ASCII编码普通的字符串'ABC'在Python内部都是ASCII编码的.Python提供了ord()和chr()函数,可以把字母和对应的数字相互转换 后来p ...
- jQuery动态添加和删除表格行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- Implement GAN from scratch
GANs from Scratch 1: A deep introduction. With code in PyTorch and TensorFlow 修改文章代码中的错误后的代码如下: impo ...
- Spring Data概览
总结:JpaRepository继承PagingAndSortingRepository,PagingAndSortingRepository继承CrudRespository,CrudResposi ...
- ORACLE ASM 日常管理
ASM概述 Automatic Storage Management(ASM)是Oracle数据库10g中一个非常出色的新特性,它以平台无关的方式提供了文件系统.逻辑卷管理器以及软件RAID等服务.A ...
- sed 删除最后几行 和删除指定行 awk使用
sed 删除最后几行 和删除指定行 转载原文链接:http://blog.51cto.com/lspgyy/1305489 sed 想删除文件中的指定行,是可以用行号指定也可以用RE来匹配的. 删 ...
- golang RPC通信读写超时设置
golang RPC通信中,有时候就怕读写hang住. 那是否可以设置读写超时呢? 1.方案一: 设置连接的读写超时 1.1 client RPC通信基于底层网络通信,可以通过设置connection ...
- pandas中根据列的值选取多行数据
# 选取等于某些值的行记录 用 == df.loc[df['column_name'] == some_value] # 选取某列是否是某一类型的数值 用 isin df.loc[df['column ...
- flutter block回调
block回调在oc中很常见,到了flutter中仍然有block回调 自定义一个StatefulWidget PageTitle 无参数回调VoidCallback VoidCallback onT ...