读取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基础教程之循环介绍
Aug 29 Python
Python 实现在文件中的每一行添加一个逗号
Apr 29 Python
python3解析库BeautifulSoup4的安装配置与基本用法
Jun 26 Python
matplotlib调整子图间距,调整整体空白的方法
Aug 03 Python
对Python正则匹配IP、Url、Mail的方法详解
Dec 25 Python
python实现蒙特卡罗方法教程
Jan 28 Python
Python数据类型之列表和元组的方法实例详解
Jul 08 Python
Python使用GitPython操作Git版本库的方法
Feb 29 Python
Python+redis通过限流保护高并发系统
Apr 15 Python
TensorFlow的环境配置与安装教程详解(win10+GeForce GTX1060+CUDA 9.0+cuDNN7.3+tensorflow-gpu 1.12.0+python3.5.5)
Jun 22 Python
python如何写个俄罗斯方块
Nov 06 Python
python使用scapy模块实现ping扫描的过程详解
Jan 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
Window下PHP三种运行方式图文详解
2013/06/11 PHP
PHP模糊查询的实现方法(推荐)
2016/09/06 PHP
php爬取天猫和淘宝商品数据
2018/02/23 PHP
php通过各种函数判断0和空
2020/07/04 PHP
Jquery和JS用外部变量获取Ajax返回的参数值的方法实例(超简单)
2013/06/17 Javascript
JS实现仿百度输入框自动匹配功能的示例代码
2014/02/19 Javascript
JavaScript function 的 length 属性使用介绍
2014/09/15 Javascript
js实现九宫格图片半透明渐显特效的方法
2015/02/16 Javascript
JavaScript实现把rgb颜色转换成16进制颜色的方法
2015/06/01 Javascript
在JavaScript应用中使用RequireJS来实现延迟加载
2015/07/01 Javascript
一种基于浏览器的自动小票机打印实现方案(js版)
2016/07/26 Javascript
JavaScript“尽快失败”的原则实例详解
2016/10/08 Javascript
jQuery用FormData实现文件上传的方法
2016/11/21 Javascript
Vue2.0组件间数据传递示例
2017/03/07 Javascript
JavaScript封装的常用工具类库bee.js用法详解【经典类库】
2018/09/03 Javascript
JS实现textarea通过换行或者回车把多行数字分割成数组并且去掉数组中空的值
2018/10/29 Javascript
[06:40]2014DOTA2西雅图国际邀请赛 DK战队巡礼
2014/07/07 DOTA
Python判断文件和文件夹是否存在的方法
2015/05/21 Python
Python中有趣在__call__函数
2015/06/21 Python
全面了解python中的类,对象,方法,属性
2016/09/11 Python
在python 中实现运行多条shell命令
2019/01/07 Python
Pycharm新建模板默认添加个人信息的实例
2019/07/15 Python
用python实现英文字母和相应序数转换的方法
2019/09/18 Python
Python如何批量获取文件夹的大小并保存
2020/03/31 Python
python实现将字符串中的数字提取出来然后求和
2020/04/02 Python
python 合并多个excel中同名的sheet
2021/01/22 Python
纯CSS3实现Material Design效果
2017/03/09 HTML / CSS
全球性的在线商店:Vogca
2019/05/10 全球购物
.net开发工程师面试题
2014/02/25 面试题
缅怀革命先烈演讲稿
2014/05/14 职场文书
植物生产学专业求职信
2014/08/08 职场文书
团队拓展活动方案
2014/08/28 职场文书
通报表扬范文
2015/01/17 职场文书
小学教师节活动总结
2015/03/20 职场文书
证婚人致辞精选
2015/07/28 职场文书
mysql 8.0.27 绿色解压版安装教程及配置方法
2022/04/20 MySQL