微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python pandas 入门 05 JSON

Pandas JSON

JSON(JavaScript Object Notation,JavaScript 对象表示法),是存储和交换文本信息的语法,类似 XML。

JSON 比 XML 更小、更快,更易解析,更多 JSON 内容可以参考 JSON 教程。

Pandas 可以很方便的处理 JSON 数据,本文以 sites.json 为例,内容如下:

实例

 
[
   {
   "id": "A001",
   "name": "教程",
   "url": "www.run.com",
   "likes": 61
   },
   {
   "id": "A002",
   "name": "Google",
   "url": "www.google.com",
   "likes": 124
   },
   {
   "id": "A003",
   "name": "淘宝",
   "url": "www.taobao.com",
   "likes": 45
   }
]

 

实例

import pandas as pd

df = pd.read_json@H_556_404@('sites.json'@H_556_404@)
   
print@H_556_404@(df.to_string@H_556_404@(@H_556_404@)@H_556_404@)

to_string() 用于返回 DataFrame 类型的数据,我们也可以直接处理 JSON 字符串。

实例

import pandas as pd

data =@H_556_404@[
    @H_556_404@{
      "id": "A001",
      "name": "教程",
      "url": "www.run.com",
      "likes": 61
    @H_556_404@},
    @H_556_404@{
      "id": "A002",
      "name": "Google",
      "url": "www.google.com",
      "likes": 124
    @H_556_404@},
    @H_556_404@{
      "id": "A003",
      "name": "淘宝",
      "url": "www.taobao.com",
      "likes": 45
    @H_556_404@}
@H_556_404@]
df = pd.DataFrame@H_556_404@(data@H_556_404@)

print@H_556_404@(df@H_556_404@)

以上实例输出结果为:

     id    name             url  likes
0  A001    教程  www.run.com     61
1  A002  Google  www.google.com    124
2  A003      淘宝  www.taobao.com     45

JSON 对象与 Python 字典具有相同的格式,所以我们可以直接将 Python 字典转化为 DataFrame 数据:

实例

import pandas as pd


# 字典格式的 JSON                                                                                              
s = @H_556_404@{
    "col1":@H_556_404@{"row1":1,"row2":2,"row3":3@H_556_404@},
    "col2":@H_556_404@{"row1":"x","row2":"y","row3":"z"@H_556_404@}
@H_556_404@}

# 读取 JSON 转为 DataFrame                                                                                          
df = pd.DataFrame@H_556_404@(s@H_556_404@)
print@H_556_404@(df@H_556_404@)

以上实例输出结果为:

      col1 col2
row1     1    x
row2     2    y
row3     3    z

从 URL 中读取 JSON 数据:

实例

import pandas as pd

URL = 'https://static.runoob.com/download/sites.json'
df = pd.read_json@H_556_404@(URL@H_556_404@)
print@H_556_404@(df@H_556_404@)

以上实例输出结果为:

     id    name             url  likes
0  A001    教程  www.run.com     61
1  A002  Google  www.google.com    124
2  A003      淘宝  www.taobao.com     45

内嵌的 JSON 数据

假设有一组内嵌的 JSON 数据文件 nested_list.json

nested_list.json 文件内容

{
    "school_name": "ABC primary school",
    "class": "Year 1",
    "students": [
    {
        "id": "A001",
        "name": "Tom",
        "math": 60,
        "physics": 66,
        "chemistry": 61
    },
    {
        "id": "A002",
        "name": "James",
        "math": 89,
        "physics": 76,
        "chemistry": 51
    },
    {
        "id": "A003",
        "name": "Jenny",
        "math": 79,
        "physics": 90,
        "chemistry": 78
    }]
}

使用以下代码格式化完整内容

实例

import pandas as pd

df = pd.read_json@H_556_404@('nested_list.json'@H_556_404@)

print@H_556_404@(df@H_556_404@)

以上实例输出结果为:

          school_name   class                                           students
0  ABC primary school  Year 1  {'id': 'A001', 'name': 'Tom', 'math': 60, 'phy...
1  ABC primary school  Year 1  {'id': 'A002', 'name': 'James', 'math': 89, 'p...
2  ABC primary school  Year 1  {'id': 'A003', 'name': 'Jenny', 'math': 79, 'p...

这时我们就需要使用到 json_normalize() 方法将内嵌的数据完整的解析出来:

实例

import pandas as pd
import json

# 使用 Python JSON 模块载入数据
with open@H_556_404@('nested_list.json','r'@H_556_404@) as f:
    data = json.loads@H_556_404@(f.read@H_556_404@(@H_556_404@)@H_556_404@)

# 展平数据
df_nested_list = pd.json_normalize@H_556_404@(data, record_path =@H_556_404@['students'@H_556_404@]@H_556_404@)
print@H_556_404@(df_nested_list@H_556_404@)

以上实例输出结果为:

     id   name  math  physics  chemistry
0  A001    Tom    60       66         61
1  A002  James    89       76         51
2  A003  Jenny    79       90         78

data = json.loads(f.read()) 使用 Python JSON 模块载入数据。

json_normalize() 使用了参数 record_path 并设置为 ['students'] 用于展开内嵌的 JSON 数据 students

显示结果还没有包含 school_name 和 class 元素,如果需要展示出来可以使用 Meta 参数来显示这些元数据:

实例

import pandas as pd
import json

# 使用 Python JSON 模块载入数据
with open@H_556_404@('nested_list.json','r'@H_556_404@) as f:
    data = json.loads@H_556_404@(f.read@H_556_404@(@H_556_404@)@H_556_404@)

# 展平数据
df_nested_list = pd.json_normalize@H_556_404@(
    data,
    record_path =@H_556_404@['students'@H_556_404@],
    Meta=@H_556_404@['school_name', 'class'@H_556_404@]
@H_556_404@)
print@H_556_404@(df_nested_list@H_556_404@)

以上实例输出结果为:

     id   name  math  physics  chemistry         school_name   class
0  A001    Tom    60       66         61  ABC primary school  Year 1
1  A002  James    89       76         51  ABC primary school  Year 1
2  A003  Jenny    79       90         78  ABC primary school  Year 1

接下来,让我们尝试读取更复杂的 JSON 数据,该数据嵌套了列表和字典,数据文件 nested_mix.json 如下:

nested_mix.json 文件内容

{
    "school_name": "local primary school",
    "class": "Year 1",
    "info": {
      "president": "John Kasich",
      "address": "ABC road, London, UK",
      "contacts": {
        "email": "[email protected]",
        "tel": "123456789"
      }
    },
    "students": [
    {
        "id": "A001",
        "name": "Tom",
        "math": 60,
        "physics": 66,
        "chemistry": 61
    },
    {
        "id": "A002",
        "name": "James",
        "math": 89,
        "physics": 76,
        "chemistry": 51
    },
    {
        "id": "A003",
        "name": "Jenny",
        "math": 79,
        "physics": 90,
        "chemistry": 78
    }]
}

nested_mix.json 文件转换为 DataFrame:

实例

import pandas as pd
import json

# 使用 Python JSON 模块载入数据
with open@H_556_404@('nested_mix.json','r'@H_556_404@) as f:
    data = json.loads@H_556_404@(f.read@H_556_404@(@H_556_404@)@H_556_404@)
   
df = pd.json_normalize@H_556_404@(
    data,
    record_path =@H_556_404@['students'@H_556_404@],
    Meta=@H_556_404@[
        'class',
        @H_556_404@['info', 'president'@H_556_404@],
        @H_556_404@['info', 'contacts', 'tel'@H_556_404@]
    @H_556_404@]
@H_556_404@)

print@H_556_404@(df@H_556_404@)

以上实例输出结果为:

     id   name  math  physics  chemistry   class info.president info.contacts.tel
0  A001    Tom    60       66         61  Year 1    John Kasich         123456789
1  A002  James    89       76         51  Year 1    John Kasich         123456789
2  A003  Jenny    79       90         78  Year 1    John Kasich         123456789

读取内嵌数据中的一组数据

以下是实例文件 nested_deep.json,我们只读取内嵌中的 math 字段:

nested_deep.json 文件内容

{
    "school_name": "local primary school",
    "class": "Year 1",
    "students": [
    {
        "id": "A001",
        "name": "Tom",
        "grade": {
            "math": 60,
            "physics": 66,
            "chemistry": 61
        }
 
    },
    {
        "id": "A002",
        "name": "James",
        "grade": {
            "math": 89,
            "physics": 76,
            "chemistry": 51
        }
       
    },
    {
        "id": "A003",
        "name": "Jenny",
        "grade": {
            "math": 79,
            "physics": 90,
            "chemistry": 78
        }
    }]
}

这里我们需要使用到 glom 模块来处理数据套嵌,glom 模块允许我们使用 . 来访问内嵌对象的属性

第一次使用我们需要安装 glom

pip3 install glom

实例

import pandas as pd
from glom import glom

df = pd.read_json@H_556_404@('nested_deep.json'@H_556_404@)

data = df@H_556_404@['students'@H_556_404@].apply@H_556_404@(lambda row: glom@H_556_404@(row, 'grade.math'@H_556_404@)@H_556_404@)
print@H_556_404@(data@H_556_404@)

以上实例输出结果为:

0    60
1    89
2    79
Name: students, dtype: int64

 

REF

https://www.runoob.com/pandas/pandas-json.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐