读取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实现用于测试网站访问速率的方法
May 26 Python
python使用arcpy.mapping模块批量出图
Mar 06 Python
window下eclipse安装python插件教程
Apr 24 Python
Python遍历pandas数据方法总结
Feb 09 Python
python调试神器PySnooper的使用
Jul 03 Python
Python使用pyautocad+openpyxl处理cad文件示例
Jul 11 Python
Python之time模块的时间戳,时间字符串格式化与转换方法(13位时间戳)
Aug 12 Python
Python pandas实现excel工作表合并功能详解
Aug 29 Python
tensorboard实现同时显示训练曲线和测试曲线
Jan 21 Python
利用python制作拼图小游戏的全过程
Dec 04 Python
python3 kubernetes api的使用示例
Jan 12 Python
使用numpy nonzero 找出非0元素
May 14 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中0,null,empty,空,false,字符串关系的详细介绍
2013/06/20 PHP
PHP cdata 处理(详细介绍)
2013/07/05 PHP
php mysql_real_escape_string函数用法与实例教程
2013/09/30 PHP
用PHP写的一个冒泡排序法的函数简单实例
2016/05/26 PHP
Ubuntu 16.04中Laravel5.4升级到5.6的步骤
2018/12/07 PHP
用javascript连接access数据库的方法
2006/11/17 Javascript
JS函数实现动态添加CSS样式表文件
2012/12/15 Javascript
JQuery中上下文选择器实现方法
2015/05/18 Javascript
详解Bootstrap插件
2016/04/25 Javascript
JavaScript实现图片瀑布流和底部刷新
2017/01/02 Javascript
JSON 数据详解及实例代码分析
2017/01/20 Javascript
基于Vue单文件组件详解
2017/09/15 Javascript
使用JS实现气泡跟随鼠标移动的动画效果
2017/09/16 Javascript
浅谈js闭包理解
2019/04/01 Javascript
vue操作动画的记录animate.css实例代码
2019/04/26 Javascript
详解在Vue.js编写更好的v-for循环的6种技巧
2020/04/14 Javascript
Linux下为不同版本python安装第三方库
2016/08/31 Python
Python实现爬取需要登录的网站完整示例
2017/08/19 Python
python实现朴素贝叶斯分类器
2018/03/28 Python
pygame游戏之旅 游戏中添加显示文字
2018/11/20 Python
python中类的输出或类的实例输出为这种形式的原因
2019/08/12 Python
简单了解python中的f.b.u.r函数
2019/11/02 Python
详解Python中的format格式化函数的使用方法
2019/11/20 Python
Python退出时强制运行一段代码的实现方法
2020/04/29 Python
python 还原梯度下降算法实现一维线性回归
2020/10/22 Python
美国最大的万圣节服装网站:HalloweenCostumes.com
2017/10/12 全球购物
分布式数据库需要考虑哪些问题
2013/12/08 面试题
Servlet面试题库
2015/07/18 面试题
期末总结的个人自我评价
2013/11/02 职场文书
社区母亲节活动记录
2014/03/06 职场文书
初中班主任评语大全
2014/04/24 职场文书
促销活动总结报告
2014/04/26 职场文书
大学生村官座谈会发言材料
2014/05/25 职场文书
校本教研活动总结
2014/07/01 职场文书
2021好看的国漫排行榜前十名 《完美世界》上榜,《元龙》排名第一
2022/03/18 国漫
GO语言字符串处理函数之处理Strings包
2022/04/14 Golang