读取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操作xml文件详细介绍
Jun 09 Python
python读取json文件并将数据插入到mongodb的方法
Mar 23 Python
python Django框架实现自定义表单提交
Mar 25 Python
python实现随机森林random forest的原理及方法
Dec 21 Python
详解pandas的外部数据导入与常用方法
May 01 Python
python and or用法详解
Jun 26 Python
Python3.0 实现决策树算法的流程
Aug 08 Python
django 做 migrate 时 表已存在的处理方法
Aug 31 Python
python:解析requests返回的response(json格式)说明
Apr 30 Python
python爬虫基础知识点整理
Jun 02 Python
python要安装在哪个盘
Jun 15 Python
浅谈Python实现opencv之图片色素的数值运算和逻辑运算
Jun 23 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
如何开发一个虚拟域名系统
2006/10/09 PHP
php猴子选大王问题解决方法
2015/05/12 PHP
详细解读php的命名空间(一)
2018/02/21 PHP
前端开发的开始---基于面向对象的Ajax类
2010/09/17 Javascript
在JavaScript中获取请求的URL参数
2010/12/22 Javascript
JS构建页面的DOM节点结构的实现代码
2011/12/09 Javascript
jQuery实现的自定义弹出层效果实例详解
2016/09/04 Javascript
JavaScript中匿名函数的递归调用
2017/01/22 Javascript
关于vue.js过渡css类名的理解(推荐)
2017/04/10 Javascript
JavaScript运动框架 多物体任意值运动(三)
2017/05/17 Javascript
js+html5实现半透明遮罩层弹框效果
2020/08/24 Javascript
nodejs前端自动化构建环境的搭建
2017/07/26 NodeJs
vue使用Element组件时v-for循环里的表单项验证方法
2018/06/28 Javascript
微信小程序导航栏滑动定位功能示例(实现CSS3的positionsticky效果)
2019/01/24 Javascript
JavaScript如何处理移动端拍摄图片旋转问题
2019/11/16 Javascript
vue路由切换时取消之前的所有请求操作
2020/09/01 Javascript
vue组件入门知识全梳理
2020/09/21 Javascript
Vue使用Element实现增删改查+打包的步骤
2020/11/25 Vue.js
js+html+css实现手动轮播和自动轮播
2020/12/30 Javascript
Python中输出ASCII大文字、艺术字、字符字小技巧
2015/04/28 Python
替换python字典中的key值方法
2018/07/06 Python
Python树莓派学习笔记之UDP传输视频帧操作详解
2019/11/15 Python
python实现飞行棋游戏
2020/02/05 Python
keras 自定义loss model.add_loss的使用详解
2020/06/22 Python
windows支持哪个版本的python
2020/07/03 Python
python 下载m3u8视频的示例代码
2020/11/11 Python
css3隔行变换色实现示例
2014/02/19 HTML / CSS
HTML5调用手机发短信和打电话功能
2020/04/29 HTML / CSS
美国工业用品采购网站:Zoro.com
2020/10/27 全球购物
营业经理岗位职责
2013/11/10 职场文书
科研先进个人典型材料
2014/01/31 职场文书
大学生先进事迹材料
2014/02/16 职场文书
四议两公开实施方案
2014/03/28 职场文书
励志演讲稿500字
2014/08/21 职场文书
教师节学生演讲稿
2014/09/03 职场文书
2014年食堂工作总结
2014/11/20 职场文书