You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pandas/Spark中访问JSON文件中的country子列

访问JSON文件中的"country"字段

根据需求,分两种常见场景给出解决方法:

1. 使用Pandas处理JSON文件

如果你的JSON是结构化表格数据,用Pandas读取后直接通过列名访问:

import pandas as pd

# 读取JSON文件
df = pd.read_json("你的文件路径.json")
# 访问country列
country_data = df["country"]
# 打印查看
print(country_data)

2. 使用Python原生JSON模块处理

如果JSON是列表包裹的对象结构,用原生模块解析后遍历提取:

import json

# 加载JSON文件
with open("你的文件路径.json", "r", encoding="utf-8") as f:
    data = json.load(f)

# 提取所有country字段
country_list = [item["country"] for item in data if "country" in item]
# 打印查看
print(country_list)

如果JSON存在多层嵌套,可根据实际结构调整取值路径,比如item["父级键名"]["country"]。

内容的提问来源于stack exchange,提问作者Another.Chemist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 21:03:11