如何将Pandas DataFrame导出到JSON文件?

2021年9月19日22:51:50 发表评论 2,227 次浏览

在本教程中,我将使用一个简单的示例向你展示如何将Pandas DataFrame导出到JSON文件。我还将查看你可能应用的不同 JSON 格式。

为了给你提供一些上下文,这里有一个模板,你可以在 Python 中使用该模板将 Pandas DataFrame导出JSON文件

df.to_json(r'Path to store the exported JSON file\File Name.json')

接下来,你将看到在实践中应用此模板的步骤。

将Pandas DataFrame导出到JSON文件的步骤

第 1 步:收集数据

假设你有以下有关不同产品及其价格的数据:

ProductPrice
Desktop Computer700
Tablet250
iPhone800
Laptop1200

目标是将上述数据导出为 JSON。

但在导出该数据之前,你需要在 Python 中捕获它。

Pandas DataFrame如何导出到JSON文件?完成此任务的一种方法是创建pandas DataFrame。

第 2 步:创建数据帧

然后,你可以使用以下代码来捕获有关产品和价格的数据:

from pandas import DataFrame

data = {'Product': ['Desktop Computer','Tablet','iPhone','Laptop'],
        'Price': [700,250,800,1200]
        }

df = DataFrame(data, columns= ['Product', 'Price'])

print (df)

在 Python 中运行代码后,你将获得此DataFrame:

如何将Pandas DataFrame导出到JSON文件?

第 3 步:将Pandas DataFrame导出到JSON文件

最后,你可以使用以下模板将 Pandas DataFrame 导出为 JSON:

df.to_json(r'Path to store the exported JSON file\File Name.json')

例如,我将存储导出的 JSON 文件的路径是:

C:\Users\Ron\Desktop\Export_DataFrame.json

你需要调整路径(在下面的Python代码),以反映的位置,你想的JSON文件存储在你的计算机:

from pandas import DataFrame

data = {'Product': ['Desktop Computer','Tablet','iPhone','Laptop'],
        'Price': [700,250,800,1200]
        }

df = DataFrame(data, columns= ['Product', 'Price'])

df.to_json (r'C:\Users\Ron\Desktop\Export_DataFrame.json')

运行代码(根据你的路径进行调整),JSON 文件将在你指定的位置创建。

你可以通过多种方式查看 JSON 内容。一种简单的方法是创建的文件拖到你的 Web 浏览器中。然后你应该得到以下结果:

{"Product":{"0":"Desktop Computer","1":"Tablet","2":"iPhone","3":"Laptop"},"Price":{"0":700,"1":250,"2":800,"3":1200}}

然后,你可以使用在线工具,例如JSON Formatter,将你的JSON 格式化为更易读的形式:

如何将Pandas DataFrame导出到JSON文件?

在下一节中,我将回顾你可能应用的不同 JSON 格式。

不同的 JSON 格式:

Pandas DataFrame如何导出到JSON文件?有多种方法可以格式化 JSON 字符串。你需要将orient设置为 所需的格式。以下是选项:

  • split
  • records
  • index
  • values
  • table
  • columns (the default format)

例如,如果你设置 orient='split',则这是一般语法的样子

df.to_json(r'Path to store the JSON file\File Name.json',orient='split')

这是我们示例的完整 Python 代码:

from pandas import DataFrame

data = {'Product': ['Desktop Computer','Tablet','iPhone','Laptop'],
        'Price': [700,250,800,1200]
        }

df = DataFrame(data, columns= ['Product', 'Price'])

df.to_json (r'C:\Users\Ron\Desktop\Export_DataFrame.json', orient='split')

运行上述代码后,你将获得以下输出:

{"columns":["Product","Price"],"index":[0,1,2,3],"data":[["Desktop Computer",700],["Tablet",250],["iPhone",800],["Laptop",1200]]}

如果你使用 JSON Formatter 格式化该输出,你将看到:

如何将Pandas DataFrame导出到JSON文件?

DataFrame导出JSON文件:在我们示例的上下文中,以下是你将获得的其他每种格式的输出:

orient=’records’

[{"Product":"Desktop Computer","Price":700},{"Product":"Tablet","Price":250},{"Product":"iPhone","Price":800},{"Product":"Laptop","Price":1200}]
如何将Pandas DataFrame导出到JSON文件?

orient=’index’

{"0":{"Product":"Desktop Computer","Price":700},"1":{"Product":"Tablet","Price":250},"2":{"Product":"iPhone","Price":800},"3":{"Product":"Laptop","Price":1200}}
如何将Pandas DataFrame导出到JSON文件?

orient=’values’

[["Desktop Computer",700],["Tablet",250],["iPhone",800],["Laptop",1200]]
如何将Pandas DataFrame导出到JSON文件?

orient=’table’

{"schema": {"fields":[{"name":"index","type":"integer"},{"name":"Product","type":"string"},{"name":"Price","type":"integer"}],"primaryKey":["index"],"pandas_version":"0.20.0"}, "data": [{"index":0,"Product":"Desktop Computer","Price":700},{"index":1,"Product":"Tablet","Price":250},{"index":2,"Product":"iPhone","Price":800},{"index":3,"Product":"Laptop","Price":1200}]}
如何将Pandas DataFrame导出到JSON文件?

orient='columns'(默认)

{"Product":{"0":"Desktop Computer","1":"Tablet","2":"iPhone","3":"Laptop"},"Price":{"0":700,"1":250,"2":800,"3":1200}}
如何将Pandas DataFrame导出到JSON文件?

你可以参考 pandas 文档以查看你可能应用的不同格式选项。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: