当前位置: 首页 > news >正文

Pandas JSON学习

1.JSON简介

    JSON(JavaScript Object Notation,JavaScript 对象表示法),是存储和交换文本信息的语法,类似 XML。JSON 比 XML 更小、更快,更易解析,Pandas 可以很方便的处理 JSON 数据。

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

    可以直接用to_string()处理 JSON 字符串。

import pandas as pddf = pd.read_json('sites.json')print(df.to_string())

import pandas as pddata =[{"id": "A001","name": "百度","url": "www.baidu.com","likes": 61},{"id": "A002","name": "Google","url": "www.google.com","likes": 124},{"id": "A003","name": "淘宝","url": "www.taobao.com","likes": 45}
]
df = pd.DataFrame(data)print(df)

2.可以直接将 Python 字典转化为 DataFrame 数据

    JSON 对象与 Python 字典具有相同的格式。

import pandas as pd# 字典格式的 JSON                                                                                             
s = {"col1":{"row1":1,"row2":2,"row3":3},"col2":{"row1":"x","row2":"y","row3":"z"}
}# 读取 JSON 转为 DataFrame                                                                                          
df = pd.DataFrame(s)print(df)

3.假设有一组内嵌的 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 pddf = pd.read_json('nested_list.json')print(df)

4.使用 json_normalize() 方法将内嵌的数据完整解析

import pandas as pd
import json# 使用 Python JSON 模块载入数据
with open('nested_list.json','r') as f:data = json.loads(f.read())# 展平数据
df_nested_list = pd.json_normalize(data, record_path =['students'])
print(df_nested_list)

    data = json.loads(f.read()) 使用 Python JSON 模块载入数据,json_normalize() 使用了参数 record_path 并设置为 ['students'] 用于展开内嵌的 JSON 数据 students。

5.使用 meta 参数显示元数据

import pandas as pd
import json# 使用 Python JSON 模块载入数据
with open('nested_list.json','r') as f:data = json.loads(f.read())# 展平数据
df_nested_list = pd.json_normalize(data,record_path =['students'],meta=['school_name', 'class']
)
print(df_nested_list)

6.假设数据文件 nested_mix.json嵌套了列表和字典

{"school_name": "local primary school","class": "Year 1","info": {"president": "John Kasich","address": "ABC road, London, UK","contacts": {"email": "admin@e.com","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}]
}

7.文件转换为 DataFrame

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

8.假设存在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}}]
}

9.使用glom 模块来处理数据套嵌

    glom 模块允许使用 . 来访问内嵌对象的属性。第一次使用需要安装 glom。

!pip install glom

import pandas as pd
from glom import glomdf = pd.read_json('nested_deep.json')data = df['students'].apply(lambda row: glom(row, 'grade.math'))
print(data)


http://www.mrgr.cn/news/63801.html

相关文章:

  • 基于Transformer的路径规划 - 第五篇 GPT生成策略_解码方法优化
  • ERROR: Failed cleaning build dir for numpy Failed > to build numpy ERROR
  • LINUX Shell命令中$0、$1-9、$#、$?、$*、$@、$!、$、$-、$IFS含义及举例
  • 语言模型|第三章|大模型训练与微调
  • eslint配置文件eslintrc.js
  • shodan3,vnc空密码批量连接,ip历史记录查找
  • 遥感辐射传输方程中的格林函数
  • PyTorch实践-CNN-手写数字识别
  • [Web安全 网络安全]-学习视频分享汇总(持续更新中)
  • 论文概览 |《Journal of Transport Geography》2024.10 Vol.120
  • Android文件选择器[超级轻量级FilePicker测试没有问题][挣扎解决自带文件管理器获取不到绝对地址问题而是返回msf%3A1000038197]
  • 二叉树的深搜
  • 防火墙技术应用
  • OpenWrt下安装Mosquitto
  • 深入解密 K 均值聚类:从理论基础到 Python 实践
  • 【Python-AI篇】seaborn
  • ConcurrentHashMap底层实现是什么
  • 算法训练(leetcode)二刷第十五天 | 654. 最大二叉树、617. 合并二叉树、700. 二叉搜索树中的搜索、98. 验证二叉搜索树
  • 凸极式发电机的相量图分析和计算,内功率因数角和外功率因数角和功角的定义。
  • AnatoMask论文汇总
  • 中国人工智能产业发展联盟发布《基于大模型的数字人系统技术要求》
  • J2:ResNet50v2算法实战与解析
  • CTF顶级工具与资源
  • 市面上12款能帮忙微信记录的数据恢复软件神器!!!
  • Python For循环
  • 再探“构造函数”