pandas.DataFrame.to_json按行轉(zhuǎn)json的方法
最近需要將csv文件轉(zhuǎn)成DataFrame并以json的形式展示到前臺(tái),故需要用到Dataframe的to_json方法
to_json方法默認(rèn)以列名為鍵,列內(nèi)容為值,形成{col1:[v11,v21,v31…],col2:[v12,v22,v32],…}這種格式,但有時(shí)我們需要按行來(lái)轉(zhuǎn)為json,形如這種格式[row1:{col1:v11,col2:v12,col3:v13…},row2:{col1:v21,col2:v22,col3:v23…}]
通過(guò)查找官網(wǎng)我們可以看到to_json方法有一個(gè)參數(shù)為orient,其參數(shù)說(shuō)明如下:
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類(lèi)似
看一下官網(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的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python中拆分具有多個(gè)分隔符的字符串方法實(shí)例
str.split()是Python中字符串類(lèi)型的一個(gè)方法,可以用來(lái)將字符串按照指定的分隔符分割成多個(gè)子字符串,這篇文章主要給大家介紹了關(guān)于Python中拆分具有多個(gè)分隔符的字符串的相關(guān)資料,需要的朋友可以參考下2023-04-04
python 基于selenium實(shí)現(xiàn)鼠標(biāo)拖拽功能
這篇文章主要介紹了python 基于selenium實(shí)現(xiàn)鼠標(biāo)拖拽功能的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12
Python創(chuàng)建Excel表和讀取Excel表的基本操作
這篇文章主要介紹了Python創(chuàng)建Excel表和讀取Excel表的基本操作,文中通過(guò)代碼示例和圖文結(jié)合的方式講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-07-07
Python實(shí)現(xiàn)分段讀取和保存遙感數(shù)據(jù)
當(dāng)遇到批量讀取大量遙感數(shù)據(jù)進(jìn)行運(yùn)算的時(shí)候,如果不進(jìn)行分段讀取操作的話,電腦內(nèi)存可能面臨著不夠使用的情況,所以我們要進(jìn)行分段讀取數(shù)據(jù)然后進(jìn)行運(yùn)算,運(yùn)算結(jié)束之后把這段數(shù)據(jù)保存成tif文件,本文介紹了Python實(shí)現(xiàn)分段讀取和保存遙感數(shù)據(jù),需要的朋友可以參考下2023-08-08
詳解用Python進(jìn)行時(shí)間序列預(yù)測(cè)的7種方法
這篇文章主要介紹了詳解用Python進(jìn)行時(shí)間序列預(yù)測(cè)的7種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Python爬蟲(chóng)之使用BeautifulSoup和Requests抓取網(wǎng)頁(yè)數(shù)據(jù)
這篇文章主要介紹了Python爬蟲(chóng)之使用BeautifulSoup和Requests抓取網(wǎng)頁(yè)數(shù)據(jù),本篇文章將介紹如何使用 Python 編寫(xiě)一個(gè)簡(jiǎn)單的網(wǎng)絡(luò)爬蟲(chóng),從網(wǎng)頁(yè)中提取有用的數(shù)據(jù),需要的朋友可以參考下2023-04-04

