pandas.DataFrame.to_json按行轉(zhuǎn)json的方法
最近需要將csv文件轉(zhuǎn)成DataFrame并以json的形式展示到前臺,故需要用到Dataframe的to_json方法
to_json方法默認(rèn)以列名為鍵,列內(nèi)容為值,形成{col1:[v11,v21,v31…],col2:[v12,v22,v32],…}這種格式,但有時(shí)我們需要按行來轉(zhuǎn)為json,形如這種格式[row1:{col1:v11,col2:v12,col3:v13…},row2:{col1:v21,col2:v22,col3:v23…}]
通過查找官網(wǎng)我們可以看到to_json方法有一個(gè)參數(shù)為orient,其參數(shù)說明如下:
orient : string Series default is ‘index' allowed values are: {‘split','records','index'} DataFrame default is ‘columns' allowed values are: {‘split','records','index','columns','values'} The format of the JSON string 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 table : dict like {‘schema': {schema}, ‘data': {data}} describing the data, and the data component is like orient='records'. Changed in version 0.20.0
大致意思為:
如果是Series轉(zhuǎn)json,默認(rèn)的orient是'index',orient可選參數(shù)有 {‘split','records','index'}
如果是DataFrame轉(zhuǎn)json,默認(rèn)的orient是'columns',orient可選參數(shù)有 {‘split','records','index','columns','values'}
json的格式如下
split,樣式為 {index -> [index], columns -> [columns], data -> [values]}
records,樣式為[{column -> value}, … , {column -> value}]
index ,樣式為 {index -> {column -> value}}
columns,樣式為 {index -> {column -> value}}
values,數(shù)組樣式
table,樣式為{‘schema': {schema}, ‘data': {data}},和records類似
看一下官網(wǎng)給的demo
df = pd.DataFrame([['a', 'b'], ['c', 'd']], index=['row 1', 'row 2'], columns=['col 1', 'col 2']) ########### split ########### df.to_json(orient='split') >'{"columns":["col 1","col 2"], "index":["row 1","row 2"], "data":[["a","b"],["c","d"]]}' ########### index ########### df.to_json(orient='index') >'{"row 1":{"col 1":"a","col 2":"b"},"row 2":{"col 1":"c","col 2":"d"}}' ########### records ########### df.to_json(orient='index') >'[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]' ########### table ########### df.to_json(orient='table') >'{"schema": {"fields": [{"name": "index", "type": "string"}, {"name": "col 1", "type": "string"}, {"name": "col 2", "type": "string"}], "primaryKey": "index", "pandas_version": "0.20.0"}, "data": [{"index": "row 1", "col 1": "a", "col 2": "b"}, {"index": "row 2", "col 1": "c", "col 2": "d"}]}'
主要參考官網(wǎng)API:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_json.html
以上這篇pandas.DataFrame.to_json按行轉(zhuǎn)json的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
pandas讀取Excel批量轉(zhuǎn)換時(shí)間戳的實(shí)踐
本文主要介紹了pandas讀取Excel批量轉(zhuǎn)換時(shí)間戳的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02python pcm音頻添加頭轉(zhuǎn)成Wav格式文件的方法
今天小編就為大家分享一篇python pcm音頻添加頭轉(zhuǎn)成Wav格式文件的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python os.mkdir()與os.makedirs()的使用區(qū)別
這篇文章主要介紹了Python os.mkdir()與os.makedirs()的使用區(qū)別,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03在Python中操作日期和時(shí)間之gmtime()方法的使用
這篇文章主要介紹了在Python中操作日期和時(shí)間之gmtime()方法的使用,是Python入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-05-05Python中用append()連接后多出一列Unnamed的解決
Python中用append()連接后多出一列Unnamed的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01Python的Django框架中的select_related函數(shù)對QuerySet 查詢的優(yōu)化
這篇文章主要介紹了Python的Django框架中的select_related函數(shù)對QuerySet查詢的優(yōu)化,以減少數(shù)據(jù)庫的查詢次數(shù)為目的,需要的朋友可以參考下2015-04-04