pandas.to_json&to_dict&from_json&from_dict解读
pandas 中的to_dict 可以对DataFrame类型的数据进行转换
可以选择六种的转换类型,分别对应于参数 ‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, ‘index’
params:
orient : str {‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, ‘index’}
Determines the type of the values of the dictionary. ‘dict’ (default) : dict like {column -> {index -> value}}
‘list’ : dict like {column -> [values]}
‘series’ : dict like {column -> Series(values)}
‘split’ : dict like {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values]}
‘records’ : list like [{column -> value}, … , {column -> value}]
‘index’ : dict like {index -> {column -> value}}
Abbreviations are allowed. s indicates series and sp indicates split. into : class, default dict
The collections.abc.Mapping subclass used for all Mappings in the return value. Can be the actual class or an empty instance of the mapping type you want.
If you want a collections.defaultdict, you must pass it initialized. return
dict, list or collections.abc.Mapping
Return a collections.abc.Mapping object representing the DataFrame. The resulting transformation depends on the orient parameter.
1、选择参数orient=’dict’
dict也是默认的参数,下面的data数据类型为DataFrame结构, 会形成 {column -> {index -> value}}这样的结构的字典,可以看成是一种双重字典结构
- 单独提取每列的值及其索引,然后组合成一个字典
- 再将上述的列属性作为关键字(key),值(values)为上述的字典
查询方式为 :data_dict[key1][key2]
- data_dict 为参数选择orient=’dict’时的数据名
- key1 为列属性的键值(外层)
- key2 为内层字典对应的键值
2、当关键字orient=’ list’ 时
和1中比较相似,只不过内层变成了一个列表,结构为{column -> [values]}
查询方式为: data_list[keys][index]
data_list 为关键字orient=’list’ 时对应的数据名
keys 为列属性的键值,如本例中的’age’ , ‘embarked’等
index 为整型索引,从0开始到最后
3、关键字参数orient=’series’
形成结构{column -> Series(values)}
调用格式为:data_series[key1][key2]或data_dict[key1]
data_series 为数据对应的名字
key1 为列属性的键值,如本例中的’age’ , ‘embarked’等
key2 使用数据原始的索引(可选)
4、关键字参数orient=’split’
形成{index -> [index], columns -> [columns], data -> [values]}的结构,是将数据、索引、属性名单独脱离出来构成字典
调用方式有 data_split[‘index’],data_split[‘data’],data_split[‘columns’]
5、当关键字orient=’records’ 时
形成[{column -> value}, … , {column -> value}]的结构
整体构成一个列表,内层是将原始数据的每行提取出来形成字典
调用格式为data_records[index][key1]
6、当关键字orient=’index’ 时
形成{index -> {column -> value}}的结构,调用格式正好和’dict’ 对应的反过来
pandas.to_json&to_dict&from_json&from_dict解读的更多相关文章
- pandas.DataFrame.where和mask 解读
1.前言背景 没怎么用过df.where 都是直接使用loc.apply等方法去解决. 可能是某些功能还没有超出loc和apply的适用范围. 2.进入df.where和df.mask DataFra ...
- 《利用Python进行数据分析: Python for Data Analysis 》学习随笔
NoteBook of <Data Analysis with Python> 3.IPython基础 Tab自动补齐 变量名 变量方法 路径 解释 ?解释, ??显示函数源码 ?搜索命名 ...
- perl json模块
JSON - JSON (JavaScript Object Notation) encoder/decoder 简介: use JSON; # imports encode_json, decode ...
- json 模块
JSON: JSON-JSON (JavaScript 对象标记) 编码/解码 简介: use JSON; # imports encode_json, decode_json, to_json an ...
- Python爱好者社区历史文章列表(每周append更新一次)
2月22日更新: 0.Python从零开始系列连载: Python从零开始系列连载(1)——安装环境 Python从零开始系列连载(2)——jupyter的常用操作 Python从零开始系列连载( ...
- [Example of Sklearn] - 分类对比
refrence :http://cloga.info/python/2014/02/07/classify_use_Sklearn/ 加载数据集 这里我使用pandas来加载数据集,数据集采用kag ...
- [Feature] Final pipeline: custom transformers
有视频:https://www.youtube.com/watch?v=BFaadIqWlAg 有代码:https://github.com/jem1031/pandas-pipelines-cust ...
- [译]使用to_dict将pandas.DataFrame转换为Python中的字典列表
pandas.DataFrame.to_json返回的是JSON字符串,不是字典. 可以使用to_dict进行字典转换. 使用orient指定方向. >>> df col1 col2 ...
- pandas 数据结构基础与转换
pandas 最常用的三种基本数据结构: 1.dataFrame: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Data ...
随机推荐
- 学习笔记:CentOS7学习之十七: Linux计划任务与日志的管理
目录 学习笔记:CentOS7学习之十七: Linux计划任务与日志的管理 17.1 计划任务-at-cron-计划任务使用方法 17.1.1 at计划任务的使用 17.1.2 查看和删除at将要执行 ...
- java核心技术(第四章)对象与类
4.1 面向对象程序设计概述 每个对象包含对用户公开的特定功能部分和隐藏的实现部分. 4.1.1类 由类构造对象的过程称为创建类的实例. 封装(数据隐藏)是与对象有关的.从形式上看,封装不过是将数据和 ...
- 小记------phoenix安装搭建
1.下载与hbase对应版本的phoenix http://phoenix.apache.org/download.html 2.解压 tar -zxvf apache-pho ...
- ubuntu14中配置tomcat8
在ubuntu14.04中配置tomcat8. 1.下载tomcat 地址:http://tomcat.apache.org/download-80.cgi ubuntu可以下载tar.gz类型的或者 ...
- 超详细,新手都能看懂 !使用SpringBoot+Dubbo 搭建一个简单的分布式服务
来自:JavaGuide Github 地址:https://github.com/Snailclimb/springboot-integration-examples 目录: 使用 SpringBo ...
- Java多线程(十):BlockingQueue实现生产者消费者模型
BlockingQueue BlockingQueue.解决了多线程中,如何高效安全"传输"数据的问题.程序员无需关心什么时候阻塞线程,什么时候唤醒线程,该唤醒哪个线程. 方法介绍 ...
- 使用Visual Studio2019 开启Openmp的方法
调试-->属性-->C/C++-->所有选项-->Openmp支持改为 是(可以使用下拉菜单) 严重性 代码 说明 项目 文件 行 禁止显示状态 禁止显示状态 错误 C2338 ...
- Api文档生成工具与Api文档的传播(pdf)
点击查看apidoc生成文档demo 1 环境和工具 win10 apidoc:注释生成api文档 wkhtmltopdf:apidoc生成的是html,不适合传播,于是通过wkhtmltopdf将h ...
- C#学习资料
http://www.runoob.com/csharp/csharp-delegate.html
- 检查一个string是否包含List<string>中的任意一个
bool b = listOfStrings.Any(s=>myString.Contains(s)); 应用在where子句中的示例: //获取路径 var groupPaths = grou ...