将JSON字符串加载到Pandas数据框中

将JSON字符串加载到Pandas数据框中

让我们看看如何在我们的Pandas DataFrame中使用JSON格式的数据集。这可以通过内置的read_json()函数来实现。它使我们能够在Pandas DataFrame中读取JSON。

例子:考虑JSON文件path_to_json.json。

将JSON字符串加载到Pandas数据框中

path_to_json.json

# importing the module
import pandas
  
# reading the file
data = df.read_json("path_to_json.json")
  
# displaying the DataFrame
print(data)
Python

输出 :

将JSON字符串加载到Pandas数据框中

现在,最终的数据框架也取决于JSON文件的类型。因此,在JSON中主要有3种类型的方向。

  • Index Oriented
  • Value-Oriented
  • Column Oriented

1. 以指数为导向

这是一个面向索引的JSON文件的例子。

将JSON字符串加载到Pandas数据框中

# importing the module
import pandas
  
# reading the file
data = df.read_json("sample.json")
  
# displaying the DataFrame
print(data)
Python

输出 :

将JSON字符串加载到Pandas数据框中

2. Value-Oriented

这是一个面向价值的JSON文件的例子。

将JSON字符串加载到Pandas数据框中

# importing the module
import pandas
  
# reading the file
data = df.read_json("sample.json")
  
# displaying the DataFrame
print(data)
Python

输出 :

将JSON字符串加载到Pandas数据框中

3.面向列的

这是一个面向列的JSON文件的例子。

将JSON字符串加载到Pandas数据框中

# importing the module
import pandas
  
# reading the file
data = df.read_json("sample.json")
  
# displaying the DataFrame
print(data)
Python

输出 :

将JSON字符串加载到Pandas数据框中

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册