读取json格式为DataFrame(可转为.csv)的实例讲解

有时候需要读取一定格式的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)的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 读取本地json文件,解析json(实例讲解)

    模拟用户登录 # data.json 文件同目录下 [ { "id": 1, "username": "zhangshan", "password": "123qwe", "lock": false }, { "id": 2, "username": "lisi", "password": "123

  • 利用python将json数据转换为csv格式的方法

    假设.json文件中存储的数据为: {"type": "Point", "link": "http://www.dianping.com/newhotel/22416995", "coordinates": [116.37256372996957, 40.39798447055443], "category": "经济型", "name": &qu

  • python读取文本中数据并转化为DataFrame的实例

    在技术问答中看到一个这样的问题,感觉相对比较常见,就单开一篇文章写下来. 从纯文本格式文件 "file_in"中读取数据,格式如下: 需要输出成"file_out",格式如下: 数据的原格式是"类别:内容",以空行"\n"为分条目,转换后变成一个条目一行,按照类别顺序依次写出内容. 建议读取后,使用pandas,把数据建立称DataFrame的表格.这样方便以后处理数据.但是原格式并不是通常的表格格式,所以要先做一些简单的处理

  • python批量读取txt文件为DataFrame的方法

    我们有时候会批量处理同一个文件夹下的文件,并且希望读取到一个文件里面便于我们计算操作.比方我有下图一系列的txt文件,我该如何把它们写入一个txt文件中并且读取为DataFrame格式呢? 首先我们要用到glob模块,这个python内置的模块可以说是非常的好用. glob.glob('*.txt') 得到如下结果: all.txt是我最后得到的结果文件.可以见返回的是一个包含txt文件名称的列表,当然如果你的文件夹下面只有txt文件,那么你用os.listdir()可以得到一个一样的列表 然后

  • 读取json格式为DataFrame(可转为.csv)的实例讲解

    有时候需要读取一定格式的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

  • ASP.NET CORE读取json格式配置文件

    目录 一.在Startup类中读取json配置文件 1.使用Configuration直接读取 2.使用IOptions接口 1.定义实体类 2.修改json文件 3.在StartUp类里面配置 3.读取自定义json文件 实例化类 添加方式1 添加方式2 二.在类库中读取json文件 在.Net Framework中,配置文件一般采用的是XML格式的,.NET Framework提供了专门的ConfigurationManager来读取配置文件的内容,.net core中推荐使用json格式的

  • 使用JSON格式提交数据到服务端的实例代码

    准备Hero.java public class Hero { private String name; private int hp; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getHp() { return hp; } public void setHp(int hp) { this.hp = hp; } @Overri

  • PHP记录和读取JSON格式日志文件

    我们有时需要记录用户或者后端的某个操作事件的运行情况,可以使用后端语言如PHP将操作结果记录到日志文件中,方便测试和查找问题.尤其是这些在后端运行的而前端不能直接看到运行结果的,那么就可以用日志文件记录下来,如果你经常跟一些接口开发如支付宝接口.微信卡券接口打交道的话,日志记录就必不可少了. 我们讲的PHP记录日志,就是将日志信息写入到一个日志文件中,区别于内存日志.写入日志的流程是:打开日志文件(如果不存在则新创建),然后将日志内容追加到日志文件的后面,最后关闭日志文件. 本文中,我们将日志内

  • 浅析Js(Jquery)中,字符串与JSON格式互相转换的示例(直接运行实例)

    首先,准备新建一个js文件.以下是JSON2.js的内容,把内容拷到js文件中,以便调用: 复制代码 代码如下: /*    http://www.JSON.org/json2.js    Public Domain.    NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.    See http://www.JSON.org/js.html    This code should be minified before deploym

  • 使用DataFrame删除行和列的实例讲解

    本文通过一个csv实例文件来展示如何删除Pandas.DataFrame的行和列 数据文件名为:example.csv 内容为: date spring summer autumn winter 2000 12.2338809 16.90730113 15.69238313 14.08596223 2001 12.84748057 16.75046873 14.51406637 13.5037456 2002 13.558175 17.2033926 15.6999475 13.23365247

  • Pandas常用的读取和保存数据的函数使用(csv,mysql,json,excel)

    pandas 是基于NumPy 的一种工具,该工具是为解决数据分析任务而创建的.Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具.Pandas的名称来自于面板数据(panel data)和python数据分析(data analysis).pandas提供了大量能使我们快速便捷地处理数据的函数和方法.它是使Python成为强大而高效的数据分析环境的重要因素之一.pandas的IO工具支持非常多的数据输入输出方式.包括csv.json.Excel.数据库等. 本

  • python如何读取和存储dict()与.json格式文件

    目录 读取和存储dict()与.json格式文件 读取.json格式文件并将数据保存到字典中 保存字典数据到.json文件中 在命令行中输出字典时的乱码问题 将字符串数据转化为字典数据 将dict数据写入json文件中 读取和存储dict()与.json格式文件 读取.json格式文件并将数据保存到字典中 数据文件:hg.json {"商家名称": "珍滋味港式粥火锅(工体店)", "评分": 27.0, "地址": &quo

  • jQuery中使用Ajax获取JSON格式数据示例代码

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.JSONM文件中包含了关于"名称"和"值"的信息.有时候我们需要读取JSON格式的数据文件,在jQuery中可以使用Ajax或者 $.getJSON()方法实现. 下面就使用jQuery读取music.txt文件中的JSON数据格式信息. 首先,music.txt中的内容如下: 复制代码 代码如下: [ {"optionKey":"1"

  • .NET Core简单读取json配置文件

    背景 目前发现网上的 .NET Core 读取 json 格式的配置文件有点麻烦,自己想搞个简单点的. .NET Core 目前的主流形式是采用 json 格式来存储配置文件信息,跟之前的诸如 app.config 和 web.config 等 xml 形式的配置文件有所区别. json 文件 demo appsettings.json: { "name": "wen", "age": 26, "family": { &quo

随机推荐