读取json格式为DataFrame(可转为.csv)的实例讲解
有时候需要读取一定格式的json文件为DataFrame,可以通过json来转换或者pandas中的read_json()。
importpandasaspd
importjson
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]
ConvertaJSONstringtopandasobject
Parameters:
path_or_buf:avalidJSONstringorfile-like,default:None
ThestringcouldbeaURL.ValidURLschemesincludehttp,ftp,s3,andfile.ForfileURLs,ahostisexpected.Forinstance,alocalfilecouldbefile://localhost/path/to/table.json
orient:string,
IndicationofexpectedJSONstringformat.CompatibleJSONstringscanbeproducedbyto_json()withacorrespondingorientvalue.Thesetofpossibleorientsis:
'split':dictlike{index->[index],columns->[columns],data->[values]}
'records':listlike[{column->value},...,{column->value}]
'index':dictlike{index->{column->value}}
'columns':dictlike{column->{index->value}}
'values':justthevaluesarray
Theallowedanddefaultvaluesdependonthevalueofthetypparameter.
whentyp=='series',
allowedorientsare{'split','records','index'}
defaultis'index'
TheSeriesindexmustbeuniquefororient'index'.
whentyp=='frame',
allowedorientsare{'split','records','index','columns','values'}
defaultis'columns'
TheDataFrameindexmustbeuniquefororients'index'and'columns'.
TheDataFramecolumnsmustbeuniquefororients'index','columns',and'records'.
typ:typeofobjecttorecover(seriesorframe),default‘frame'
dtype:booleanordict,defaultTrue
IfTrue,inferdtypes,ifadictofcolumntodtype,thenusethose,ifFalse,thendon'tinferdtypesatall,appliesonlytothedata.
convert_axes:boolean,defaultTrue
Trytoconverttheaxestotheproperdtypes.
convert_dates:boolean,defaultTrue
Listofcolumnstoparsefordates;IfTrue,thentrytoparsedatelikecolumnsdefaultisTrue;acolumnlabelisdatelikeif
itendswith'_at',
itendswith'_time',
itbeginswith'timestamp',
itis'modified',or
itis'date'
keep_default_dates:boolean,defaultTrue
Ifparsingdates,thenparsethedefaultdatelikecolumns
numpy:boolean,defaultFalse
Directdecodingtonumpyarrays.Supportsnumericdataonly,butnon-numericcolumnandindexlabelsaresupported.NotealsothattheJSONorderingMUSTbethesameforeachtermifnumpy=True.
precise_float:boolean,defaultFalse
Settoenableusageofhigherprecision(strtod)functionwhendecodingstringtodoublevalues.Default(False)istousefastbutlessprecisebuiltinfunctionality
date_unit:string,defaultNone
Thetimestampunittodetectifconvertingdates.Thedefaultbehaviouristotryanddetectthecorrectprecision,butifthisisnotdesiredthenpassoneof‘s',‘ms',‘us'or‘ns'toforceparsingonlyseconds,milliseconds,microsecondsornanosecondsrespectively.
lines:boolean,defaultFalse
Readthefileasajsonobjectperline.
Newinversion0.19.0.
encoding:str,defaultis‘utf-8'
Theencodingtousetodecodepy3bytes.
Newinversion0.19.0.
以上这篇读取json格式为DataFrame(可转为.csv)的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。