读取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中wx将图标显示在右下角的脚本代码
Mar 08 Python
python使用socket进行简单网络连接的方法
Apr 29 Python
python中的错误处理
Apr 10 Python
python实现将读入的多维list转为一维list的方法
Jun 28 Python
Tensorflow 合并通道及加载子模型的方法
Jul 26 Python
对Python3 pyc 文件的使用详解
Feb 16 Python
pyQT5 实现窗体之间传值的示例
Jun 20 Python
python批量修改图片尺寸,并保存指定路径的实现方法
Jul 04 Python
python matplotlib库绘制条形图练习题
Aug 10 Python
Python自定义聚合函数merge与transform区别详解
May 26 Python
Django Model层F,Q对象和聚合函数原理解析
Nov 12 Python
Python用requests库爬取返回为空的解决办法
Feb 21 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 adodb连接带密码access数据库实例,测试成功
2008/05/14 PHP
php中将指针移动到数据集初始位置的实现代码[mysql_data_seek]
2012/11/01 PHP
php上传图片之时间戳命名(保存路径)
2014/08/15 PHP
php 中的信号处理操作实例详解
2020/03/04 PHP
PHP基于phpqrcode类生成二维码的方法示例详解
2020/08/07 PHP
js 文件引入实现代码
2010/04/23 Javascript
js FLASH幻灯片字符串中有连接符&的处理方法
2012/03/01 Javascript
JQuery球队选择实例
2015/05/18 Javascript
JS+CSS实现简易的滑动门效果代码
2015/09/24 Javascript
JavaScript中获取HTML元素值的三种方法
2016/06/20 Javascript
使用json来定义函数,在里面可以定义多个函数的实现方法
2016/10/28 Javascript
Bootstrap CSS组件之导航(nav)
2016/12/17 Javascript
bootstrap提示标签、提示框实现代码
2016/12/28 Javascript
axios学习教程全攻略
2017/03/26 Javascript
jfinal与bootstrap的登出实战详解
2017/11/27 Javascript
JavaScript中 ES6变量的结构赋值
2018/07/10 Javascript
python获取文件版本信息、公司名和产品名的方法
2014/10/05 Python
分析用Python脚本关闭文件操作的机制
2015/06/28 Python
python 列表降维的实例讲解
2018/06/28 Python
python事件驱动event实现详解
2018/11/21 Python
wxPython色环电阻计算器
2019/11/18 Python
Python中remove漏删和索引越界问题的解决
2020/03/18 Python
基于selenium及python实现下拉选项定位select
2020/07/22 Python
python 实时调取摄像头的示例代码
2020/11/25 Python
Django中使用Celery的方法步骤
2020/12/07 Python
html5之Canvas路径绘图、坐标变换应用实例
2012/12/26 HTML / CSS
哥伦比亚最大的网上商店:Linio哥伦比亚
2016/09/25 全球购物
师范应届生求职信
2013/11/15 职场文书
团日活动总结范文
2014/04/25 职场文书
市场拓展计划书
2014/05/03 职场文书
捐款倡议书怎么写
2014/05/13 职场文书
安全标语口号
2014/06/09 职场文书
医院节能减排方案
2014/06/13 职场文书
2015年度优秀员工推荐信
2015/03/23 职场文书
食堂管理制度范本
2015/08/04 职场文书
详解SQL报错盲注
2022/07/23 SQL Server