欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-01-23 16:23:16
有时候需要读取一定格式的json文件为dataframe,可以通过json来转换或者pandas中的read_json()。 import pandas as p...

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