读取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脚本实现统计日志文件中的ip访问次数代码分享
Aug 06 Python
python 多线程实现检测服务器在线情况
Nov 25 Python
在MAC上搭建python数据分析开发环境
Jan 26 Python
python制作websocket服务器实例分享
Nov 20 Python
Python安装lz4-0.10.1遇到的坑
May 20 Python
python查看列的唯一值方法
Jul 17 Python
Pandas透视表(pivot_table)详解
Jul 22 Python
Windows下Pycharm远程连接虚拟机中Centos下的Python环境(图文教程详解)
Mar 19 Python
Python matplotlib实时画图案例
Apr 23 Python
python实现最短路径的实例方法
Jul 19 Python
Python使用eval函数执行动态标表达式过程详解
Oct 17 Python
python 三种方法提取pdf中的图片
Feb 07 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面向对象概念
2011/11/06 PHP
使用PHP静态变量当缓存的方法
2013/11/13 PHP
php的RSA加密解密算法原理与用法分析
2020/01/23 PHP
$()JS小技巧
2007/07/21 Javascript
jquery 可拖拽的窗体控件实现代码
2010/03/21 Javascript
JS简单实现文件上传实例代码(无需插件)
2013/11/15 Javascript
nodejs URL模块操作URL相关方法介绍
2015/03/03 NodeJs
快速学习jQuery插件 Form表单插件使用方法
2015/12/01 Javascript
复杂的javascript窗口分帧解析
2016/02/19 Javascript
JQuery实现简单的服务器轮询效果实例
2016/03/31 Javascript
js实现纯前端的图片预览
2016/04/27 Javascript
vue滚动轴插件better-scroll使用详解
2017/10/17 Javascript
Vuejs实现购物车功能
2017/11/05 Javascript
利用jqgrid实现上移下移单元格功能
2018/11/07 Javascript
JavaScript学习笔记之基于定时器实现图片无缝滚动功能详解
2019/01/09 Javascript
9102年webpack4搭建vue项目的方法步骤
2019/02/20 Javascript
VUE+elementui组件在table-cell单元格中绘制微型echarts图
2020/04/20 Javascript
JavaScript实现alert弹框效果
2020/11/19 Javascript
vue项目配置 webpack-obfuscator 进行代码加密混淆的实现
2021/02/26 Vue.js
[02:19]2014DOTA2国际邀请赛 专访820少年们一起去追梦吧
2014/07/14 DOTA
Python 正则表达式匹配字符串中的http链接方法
2018/12/25 Python
Opencv-Python图像透视变换cv2.warpPerspective的示例
2019/04/11 Python
python生成大写32位uuid代码
2020/03/03 Python
Python操作MongoDb数据库流程详解
2020/03/05 Python
Python第三方库的几种安装方式(小结)
2020/04/03 Python
使用python创建Excel工作簿及工作表过程图解
2020/05/27 Python
python获取linux系统信息的三种方法
2020/10/14 Python
python 制作网站筛选工具(附源码)
2021/01/21 Python
python 高阶函数简单介绍
2021/02/19 Python
COS美国官网:知名服装品牌
2019/04/08 全球购物
什么是SCM(软件配置管理)
2014/08/16 面试题
支部书记四风问题自我剖析材料
2014/09/29 职场文书
2014年预备党员群众路线教育实践活动对照检查材料思想汇报
2014/10/02 职场文书
2014年林业工作总结
2014/12/05 职场文书
关于空气污染危害的感想
2015/08/11 职场文书
2016年安全月活动总结
2016/04/06 职场文书