读取json格式为DataFrame(可转为.csv)的实例讲解


Posted in Python onJune 05, 2018

有时候需要读取一定格式的json文件为DataFrame,可以通过json来转换或者pandas中的read_json()。

import pandas as pd
import json
data = pd.DataFrame(json.loads(open('jsonFile.txt','r+').read()))#方法一
dataCopy = pd.read_json('jsonFile.txt',typ='frame') #方法二
pandas.read_json(path_or_buf=None, orient=None, typ='frame', dtype=True, convert_axes=True, convert_dates=True, keep_default_dates=True, numpy=False, precise_float=False, date_unit=None, encoding=None, lines=False)[source]
 Convert a JSON string to pandas object
 Parameters: 
 path_or_buf : a valid JSON string or file-like, default: None
 The string could be a URL. Valid URL schemes include http, ftp, s3, and file. For file URLs, a host is expected. For instance, a local file could be file://localhost/path/to/table.json
 orient : string,
 Indication of expected JSON string format. Compatible JSON strings can be produced by to_json() with a corresponding orient value. The set of possible orients is:
  'split' : dict like {index -> [index], columns -> [columns], data -> [values]}
  'records' : list like [{column -> value}, ... , {column -> value}]
  'index' : dict like {index -> {column -> value}}
  'columns' : dict like {column -> {index -> value}}
  'values' : just the values array
 The allowed and default values depend on the value of the typ parameter.
  when typ == 'series',
  allowed orients are {'split','records','index'}
  default is 'index'
  The Series index must be unique for orient 'index'.
  when typ == 'frame',
  allowed orients are {'split','records','index', 'columns','values'}
  default is 'columns'
  The DataFrame index must be unique for orients 'index' and 'columns'.
  The DataFrame columns must be unique for orients 'index', 'columns', and 'records'.
 typ : type of object to recover (series or frame), default ‘frame'
 dtype : boolean or dict, default True
 If True, infer dtypes, if a dict of column to dtype, then use those, if False, then don't infer dtypes at all, applies only to the data.
 convert_axes : boolean, default True
 Try to convert the axes to the proper dtypes.
 convert_dates : boolean, default True
 List of columns to parse for dates; If True, then try to parse datelike columns default is True; a column label is datelike if
  it ends with '_at',
  it ends with '_time',
  it begins with 'timestamp',
  it is 'modified', or
  it is 'date'
 keep_default_dates : boolean, default True
 If parsing dates, then parse the default datelike columns
 numpy : boolean, default False
 Direct decoding to numpy arrays. Supports numeric data only, but non-numeric column and index labels are supported. Note also that the JSON ordering MUST be the same for each term if numpy=True.
 precise_float : boolean, default False
 Set to enable usage of higher precision (strtod) function when decoding string to double values. Default (False) is to use fast but less precise builtin functionality
 date_unit : string, default None
 The timestamp unit to detect if converting dates. The default behaviour is to try and detect the correct precision, but if this is not desired then pass one of ‘s', ‘ms', ‘us' or ‘ns' to force parsing only seconds, milliseconds, microseconds or nanoseconds respectively.
 lines : boolean, default False
 Read the file as a json object per line.
 New in version 0.19.0.
 encoding : str, default is ‘utf-8'
 The encoding to use to decode py3 bytes.
 New in version 0.19.0.

以上这篇读取json格式为DataFrame(可转为.csv)的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python 冒泡,选择,插入排序使用实例
Feb 05 Python
Python中输出ASCII大文字、艺术字、字符字小技巧
Apr 28 Python
CentOS安装pillow报错的解决方法
Jan 27 Python
Python生成随机密码的方法
Jun 16 Python
Python开发中爬虫使用代理proxy抓取网页的方法示例
Sep 26 Python
Python把对应格式的csv文件转换成字典类型存储脚本的方法
Feb 12 Python
python实现趣味图片字符化
Apr 30 Python
Python实现计算文件MD5和SHA1的方法示例
Jun 11 Python
python搜索包的路径的实现方法
Jul 19 Python
Python函数的返回值、匿名函数lambda、filter函数、map函数、reduce函数用法实例分析
Dec 26 Python
python归并排序算法过程实例讲解
Nov 04 Python
python Scrapy框架原理解析
Jan 04 Python
Python实现迭代时使用索引的方法示例
Jun 05 #Python
Numpy 将二维图像矩阵转换为一维向量的方法
Jun 05 #Python
django反向解析和正向解析的方式
Jun 05 #Python
Python numpy实现二维数组和一维数组拼接的方法
Jun 05 #Python
Python实现字典(dict)的迭代操作示例
Jun 05 #Python
python矩阵转换为一维数组的实例
Jun 05 #Python
python验证码识别教程之利用滴水算法分割图片
Jun 05 #Python
You might like
php is_file 判断给定文件名是否为一个正常的文件
2010/05/10 PHP
PHP和JAVA中的重载(overload)和覆盖(override) 介绍
2012/03/01 PHP
ThinkPHP3.1数据CURD操作快速入门
2014/06/19 PHP
基于Asp.net与Javascript控制的日期控件
2010/05/22 Javascript
使用jquery与图片美化checkbox和radio控件的代码(打包下载)
2010/11/11 Javascript
Javascript 检测键盘按键信息及键码值对应介绍
2013/01/03 Javascript
jQuery 遍历map()方法详解
2016/11/04 Javascript
jquery+html仿翻页相册功能
2016/12/20 Javascript
Bootstrap笔记—折叠实例代码
2017/03/13 Javascript
简单了解JavaScript中常见的反模式
2019/06/21 Javascript
JS通过识别id、value值对checkbox设置选中状态
2020/02/19 Javascript
[02:16]DOTA2超级联赛专访Burning 逆袭需要抓住机会
2013/06/24 DOTA
让python的Cookie.py模块支持冒号做key的方法
2010/12/28 Python
用C++封装MySQL的API的教程
2015/05/06 Python
python套接字流重定向实例汇总
2016/03/03 Python
关于Python 3中print函数的换行详解
2017/08/08 Python
Python标准库之itertools库的使用方法
2017/09/07 Python
Python matplotlib 画图窗口显示到gui或者控制台的实例
2018/05/24 Python
Python3随机漫步生成数据并绘制
2018/08/27 Python
Django 模型类(models.py)的定义详解
2019/07/19 Python
python GUI图形化编程wxpython的使用
2019/07/19 Python
python循环嵌套的多种使用方法解析
2019/11/29 Python
Python while循环使用else语句代码实例
2020/02/07 Python
基于Tensorflow一维卷积用法详解
2020/05/22 Python
Python用户自定义异常的实现
2020/12/25 Python
HTML5: Web 标准最巨大的飞跃
2008/10/17 HTML / CSS
Derek Rose官网:英国高档睡衣、家居服和内衣品牌
2020/01/18 全球购物
逻辑链路控制协议
2016/10/01 面试题
新郎新娘婚礼答谢词
2014/01/11 职场文书
挂牌仪式主持词
2014/03/20 职场文书
岗位说明书怎么写
2014/07/30 职场文书
农行心得体会
2014/09/02 职场文书
婚宴邀请函
2015/01/30 职场文书
2015年组织部工作总结
2015/04/03 职场文书
单位更名证明
2015/06/18 职场文书
导游词之西安大清真寺
2019/12/17 职场文书