將 Pandas DataFrame 轉換為 JSON

Manav Narula 2023年1月30日
  1. orient = 'columns'
  2. orient = 'records'
  3. orient = 'index'
  4. orient = 'split'
  5. orient = 'table'
將 Pandas DataFrame 轉換為 JSON

JSON 是 JavaScript Object Notation 的縮寫,它是基於 JavaScript 中物件的格式,是一種表示結構化資料的編碼技術。它是基於 JavaScript 中物件的格式,是一種表示結構化資料的編碼技術。現在它被廣泛使用,特別是在伺服器和 Web 應用程式之間共享資料。

我們將在本文中介紹如何將 DataFrame 轉換為 JSON 字串。

我們將使用以下 DataFrame。

import pandas as pd

df = pd.DataFrame(
    [["Jay", 16, "BBA"], ["Jack", 19, "BTech"], ["Mark", 18, "BSc"]],
    columns=["Name", "Age", "Course"],
)

print(df)

輸出:

   Name  Age Course
0   Jay   16    BBA
1  Jack   19  BTech
2  Mark   18    BSc

Pandas DataFrame 有一個方法 dataframe.to_json(),它可以將 DataFrame 轉換為 JSON 字串或儲存為外部 JSON 檔案。最終的 JSON 格式取決於 orient 引數的值,預設情況下是'columns',但也可以指定為'records''index''split''table''values'

所有的格式將在下面介紹。

orient = 'columns'

import pandas as pd

df = pd.DataFrame(
    [["Jay", 16, "BBA"], ["Jack", 19, "BTech"], ["Mark", 18, "BSc"]],
    columns=["Name", "Age", "Course"],
)

js = df.to_json(orient="columns")

print(js)

輸出:

{"Name":{"0":"Jay","1":"Jack","2":"Mark"},
 "Age":{"0":16,"1":19,"2":18},
 "Course":{"0":"BBA","1":"BTech","2":"BSc"}}

orient = 'records'

import pandas as pd

df = pd.DataFrame(
    [["Jay", 16, "BBA"], ["Jack", 19, "BTech"], ["Mark", 18, "BSc"]],
    columns=["Name", "Age", "Course"],
)

js = df.to_json(orient="records")

print(js)

輸出:

[{"Name":"Jay","Age":16,"Course":"BBA"},{"Name":"Jack","Age":19,"Course":"BTech"},{"Name":"Mark","Age":18,"Course":"BSc"}]

orient = 'index'

import pandas as pd

df = pd.DataFrame(
    [["Jay", 16, "BBA"], ["Jack", 19, "BTech"], ["Mark", 18, "BSc"]],
    columns=["Name", "Age", "Course"],
)

js = df.to_json(orient="index")

print(js)

輸出:

{"0":{"Name":"Jay","Age":16,"Course":"BBA"},
 "1":{"Name":"Jack","Age":19,"Course":"BTech"},
 "2":{"Name":"Mark","Age":18,"Course":"BSc"}}

orient = 'split'

import pandas as pd

df = pd.DataFrame(
    [["Jay", 16, "BBA"], ["Jack", 19, "BTech"], ["Mark", 18, "BSc"]],
    columns=["Name", "Age", "Course"],
)

js = df.to_json(orient="split")

print(js)

輸出:

{"columns":["Name","Age","Course"],
 "index":[0,1,2],
 "data":[["Jay",16,"BBA"],["Jack",19,"BTech"],["Mark",18,"BSc"]]}

orient = 'table'

import pandas as pd

df = pd.DataFrame(
    [["Jay", 16, "BBA"], ["Jack", 19, "BTech"], ["Mark", 18, "BSc"]],
    columns=["Name", "Age", "Course"],
)

js = df.to_json(orient="table")

print(js)

輸出:

{"schema": {"fields":[{"name":"index","type":"integer"},{"name":"Name","type":"string"},{"name":"Age","type":"integer"},{"name":"Course","type":"string"}],"primaryKey":["index"],"pandas_version":"0.20.0"}, "data": [{"index":0,"Name":"Jay","Age":16,"Course":"BBA"},{"index":1,"Name":"Jack","Age":19,"Course":"BTech"},{"index":2,"Name":"Mark","Age":18,"Course":"BSc"}]}

如前所述,我們還可以直接將 JSON 輸出到外部檔案。它可以通過在 dataframe.to_json() 函式中提供檔案的路徑來實現,如下所示。

import pandas as pd

df = pd.DataFrame(
    [["Jay", 16, "BBA"], ["Jack", 19, "BTech"], ["Mark", 18, "BSc"]],
    columns=["Name", "Age", "Course"],
)

df.to_json("path\example.json", orient="table")

上面的程式碼將 JSON 檔案匯出到指定的路徑。

作者: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn

相關文章 - Pandas DataFrame

相關文章 - Pandas JSON