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)