시작하기무료로 시작하기

pandas로 데이터 그룹화하기

데이터 파이프라인의 출력은 일반적으로 "모델링된" 데이터세트입니다. 이 데이터세트는 데이터 소비자에게 최소한의 조작만으로도 정보를 쉽게 제공해 줍니다. pandas로 데이터를 그룹화하면 모델링된 데이터세트를 만드는 데 도움이 됩니다.

pandaspd로 임포트되어 있고, 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" 열만 유지하세요.
  • DataFrame을 "city" 열로 그룹화하고, 각 도시의 math, reading, writing 점수 평균을 구하세요.
  • 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())
코드 편집 및 실행