当前位置: 首页 > 编程笔记 >

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

茅和玉
2023-03-14
本文向大家介绍读取json格式为DataFrame(可转为.csv)的实例讲解,包括了读取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)的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持小牛知识库。

 类似资料:
  • 问题内容: 我尝试使用json格式的文件作为输入。这是示例数据的片段。 可以在r中使用这种复杂的json格式制作一个csv,以便更平滑地处理数据吗? 例如,有以下基本类别:基本信息照片创建者位置类别网址 可以制作带有basic_information.id,creator.id等子类别类别的csv文件吗? 问题答案: 在研究您的答案时,我在评论中张贴了一些链接,但现在我非常确信这是解决问题的方法。

  • 问题内容: 我有一个带有两列的Pandas –一列带有文件名,一列带有生成它的时间: 我正在尝试将其转换为以下格式的JSON文件: 使用命令时,将以以下格式获取记录: 我只是想知道是否存在以所需格式获取JSON文件的选项。任何帮助,将不胜感激。 问题答案: 您得到的输出是。因此,您可以根据需要简单地对其进行切片,并从中删除逗号。 要将输出写入文本文件,您可以执行以下操作:

  • 问题内容: 我收到了JSON文件,但不知道如何读取。有转换器可以在其中生成漂亮的CSV文件,以便将其加载到MS Excel中吗?我不懂JSON,所以如果有人编写脚本或将我链接到可以完成此任务的脚本,那将非常棒。 我在http://json.bloople.net上找到了一些接近的东西,但是不幸的是,它是JSON到HTML。 编辑:jsonformat.com越来越近,但是它仍然不是CSV。 问题答

  • 我得到了一个CSV文件和一个头文件,它必须通过Spark(2.0.0和Scala2.11.8)作为数据frame读取。 是否有任何方法可以使用spark代码仅从CSV头中转义特殊字符?

  • 本文向大家介绍python将ansible配置转为json格式实例代码,包括了python将ansible配置转为json格式实例代码的使用技巧和注意事项,需要的朋友参考一下 python将ansible配置转为json格式实例代码 ansible的配置文件举例如下,这种配置文件不利于在前端的展现,因此,我们用一段简单的代码将ansible的配置文件转为json格式的: 用python将ansib

  • 我收到的csv文件格式不正确(无法控制生成此csv的应用程序) CSV的标题和第一行如下所示: 这是我用来读取csv的代码: 这是我收到的输出: 第一个问题是奇怪的字符(可能缺少编码选项?)另外,标题是错误的,不能在该格式上使用DictReader,这对于编辑CSV非常有用。 我可以重写一个新的CSV与标题正确格式化,这不是一个问题,但我不知道如何跳过CSV的前3行!?或者我可以用CSV即将到来的