欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pandas.DataFrame.to_json按行轉(zhuǎn)json的方法

 更新時(shí)間:2018年06月05日 09:42:51   作者:huanbia  
今天小編就為大家分享一篇pandas.DataFrame.to_json按行轉(zhuǎn)json的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

最近需要將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í)踐

    本文主要介紹了pandas讀取Excel批量轉(zhuǎn)換時(shí)間戳的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • python pcm音頻添加頭轉(zhuǎn)成Wav格式文件的方法

    python pcm音頻添加頭轉(zhuǎn)成Wav格式文件的方法

    今天小編就為大家分享一篇python pcm音頻添加頭轉(zhuǎn)成Wav格式文件的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python os.mkdir()與os.makedirs()的使用區(qū)別

    Python os.mkdir()與os.makedirs()的使用區(qū)別

    這篇文章主要介紹了Python os.mkdir()與os.makedirs()的使用區(qū)別,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • python if not in 多條件判斷代碼

    python if not in 多條件判斷代碼

    學(xué)了一段時(shí)間python,發(fā)現(xiàn)有時(shí)遇到多條件判斷的時(shí)候,覺得使用單純的in的話比較麻煩,需要多個(gè)條件,今天又遇到一個(gè),網(wǎng)上搜索了一下,也有同學(xué)和我遇到相同的問題,記錄一下
    2016-09-09
  • 在Python中操作日期和時(shí)間之gmtime()方法的使用

    在Python中操作日期和時(shí)間之gmtime()方法的使用

    這篇文章主要介紹了在Python中操作日期和時(shí)間之gmtime()方法的使用,是Python入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-05-05
  • Python中用append()連接后多出一列Unnamed的解決

    Python中用append()連接后多出一列Unnamed的解決

    Python中用append()連接后多出一列Unnamed的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Python的Django框架中的select_related函數(shù)對QuerySet 查詢的優(yōu)化

    Python的Django框架中的select_related函數(shù)對QuerySet 查詢的優(yōu)化

    這篇文章主要介紹了Python的Django框架中的select_related函數(shù)對QuerySet查詢的優(yōu)化,以減少數(shù)據(jù)庫的查詢次數(shù)為目的,需要的朋友可以參考下
    2015-04-04
  • python多線程分塊讀取文件

    python多線程分塊讀取文件

    這篇文章主要為大家詳細(xì)介紹了python多線程分塊讀取文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • python接口,繼承,重載運(yùn)算符詳解

    python接口,繼承,重載運(yùn)算符詳解

    這篇文章主要給大家介紹了關(guān)于Python接口,繼承,重載運(yùn)算符的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • 對python程序內(nèi)存泄漏調(diào)試的記錄

    對python程序內(nèi)存泄漏調(diào)試的記錄

    今天小編就為大家分享一篇對python程序內(nèi)存泄漏調(diào)試的記錄,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06

最新評論