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)