将 Pandas DataFrame 转换为 JSON

Manav Narula 2023年1月30日 2020年12月31日
  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 文件导出到指定的路径。

Author: 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