pandas로 데이터 그룹화하기
데이터 파이프라인의 결과물은 일반적으로 "모델링된" 데이터셋입니다. 이 데이터셋을 사용하면 데이터 소비자가 별도의 가공 작업 없이도 정보에 쉽게 접근할 수 있습니다. pandas로 데이터를 그룹화하면 이러한 모델링된 데이터셋을 만드는 데 도움이 됩니다.
pandas는 pd로 임포트되어 있으며, raw_testing_scores DataFrame은 다음과 같은 형태의 데이터를 포함하고 있습니다:
street_address city math_score reading_score writing_score
01M539 111 Columbia Street Manhattan 657.0 601.0 601.0
02M294 350 Grand Street Manhattan 395.0 411.0 387.0
02M308 350 Grand Street Manhattan 418.0 428.0 415.0
이 연습은 강의의 일부입니다
Python으로 하는 ETL과 ELT
연습 안내
.loc[]를 사용해"city","math_score","reading_score","writing_score"열만 남기세요."city"열을 기준으로 DataFrame을 그룹화하고, 도시별 수학, 읽기, 쓰기 점수의 평균을 구하세요.transform()함수를 사용해 그룹화된 DataFrame을 만드세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
def transform(raw_data):
# Use .loc[] to only return the needed columns
raw_data = raw_data.____[:, ____]
# Group the data by city, return the grouped DataFrame
grouped_data = raw_data.____(by=["____"], axis=0).____()
return grouped_data
# Transform the data, print the head of the DataFrame
grouped_testing_scores = ____(raw_testing_scores)
print(grouped_testing_scores.head())