开始使用免费开始使用

转换 JSON 数据

当把 JSON 格式的数据读入字典时,通常需要在存入 DataFrame 之前对数据进行一定的手动转换。处理嵌套字典时尤其常见。本练习将带您进行相关探索。

文件 "nested_school_scores.json" 已读入为字典,保存在变量 raw_testing_scores 中,其结构如下所示:

{
    "01M539": {
        "street_address": "111 Columbia Street",
        "city": "Manhattan",
        "scores": {
              "math": 657,
              "reading": 601,
              "writing": 601
        }
  }, ...
}

本练习是课程的一部分

使用 Python 的 ETL 和 ELT

查看课程

练习说明

  • 同时遍历 raw_testing_scores 字典的键和值。
  • raw_testing_scores 对象中每个嵌套的字典里提取 "street_address"

交互式实操练习

通过完成这段示例代码来试试这个练习。

normalized_testing_scores = []

# Loop through each of the dictionary key-value pairs
for school_id, school_info in raw_testing_scores.____():
	normalized_testing_scores.append([
    	school_id,
    	school_info.____("____"),  # Pull the "street_address"
    	school_info.get("city"),
    	school_info.get("scores").get("math", 0),
    	school_info.get("scores").get("reading", 0),
    	school_info.get("scores").get("writing", 0),
    ])

print(normalized_testing_scores)
编辑并运行代码