读取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脚本实现xls(xlsx)转成csv
Apr 10 Python
Python django实现简单的邮件系统发送邮件功能
Jul 14 Python
基于python(urlparse)模板的使用方法总结
Oct 13 Python
Python数据分析之双色球统计两个红和蓝球哪组合比例高的方法
Feb 03 Python
matlab中实现矩阵删除一行或一列的方法
Apr 04 Python
Pycharm代码无法复制,无法选中删除,无法编辑的解决方法
Oct 22 Python
正确理解Python中if __name__ == '__main__'
Jan 24 Python
python list转置和前后反转的例子
Aug 26 Python
python中调试或排错的五种方法示例
Sep 12 Python
聊聊python在linux下与windows下导入模块的区别说明
Mar 03 Python
Python获取百度热搜的完整代码
Apr 07 Python
python 办公自动化——基于pyqt5和openpyxl统计符合要求的名单
May 25 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
linux下为php添加curl扩展的方法
2011/07/29 PHP
javaScript 判断字符串是否为数字的简单方法
2009/07/25 Javascript
jquery简单实现鼠标经过导航条改变背景图
2013/12/17 Javascript
jQuery根据元素值删除数组元素的方法
2015/06/24 Javascript
jQuery插件formValidator自定义函数扩展功能实例详解
2015/11/25 Javascript
AngularJS 如何在控制台进行错误调试
2016/06/07 Javascript
jQuery+正则+文本框只能输入数字的实现方法
2016/10/07 Javascript
jstl中判断list中是否包含某个值的简单方法
2016/10/14 Javascript
js实现自动轮换选项卡
2017/01/13 Javascript
从零学习node.js之利用express搭建简易论坛(七)
2017/02/25 Javascript
VueJS如何引入css或者less文件的一些坑
2017/04/25 Javascript
vue加载自定义的js文件方法
2018/03/13 Javascript
vue+element-ui实现表格编辑的三种实现方式
2018/10/31 Javascript
JavaScript使用面向对象实现的拖拽功能详解
2019/06/12 Javascript
vue history 模式打包部署在域名的二级目录的配置指南
2019/07/02 Javascript
微信小程序基于movable-view实现滑动删除效果
2020/01/08 Javascript
使用Vue 自定义文件选择器组件的实例代码
2020/03/04 Javascript
vue 判断元素内容是否超过宽度的方式
2020/07/29 Javascript
vue实现移动端拖动排序
2020/08/21 Javascript
Python使用plotly绘制数据图表的方法
2017/07/18 Python
python得到电脑的开机时间方法
2018/10/15 Python
python实现大文本文件分割
2019/07/22 Python
django formset实现数据表的批量操作的示例代码
2019/12/06 Python
python GUI库图形界面开发之PyQt5表单布局控件QFormLayout详细使用方法与实例
2020/03/06 Python
美国睫毛、眉毛精华液领导品牌:RevitaLash Cosmetics
2018/03/26 全球购物
网上签名寄语活动留言
2014/01/18 职场文书
我爱我家教学反思
2014/05/01 职场文书
教师敬业奉献模范事迹材料
2014/05/18 职场文书
安全横幅标语
2014/06/09 职场文书
城市创卫标语
2014/06/17 职场文书
地球一小时宣传标语
2014/06/24 职场文书
班子成员四风问题自我剖析材料
2014/09/29 职场文书
贪污受贿检讨书范文
2014/11/19 职场文书
物业保洁员管理制度
2015/08/05 职场文书
PHP策略模式写法
2021/04/01 PHP
python opencv通过4坐标剪裁图片
2021/06/05 Python