DataFrame에 고급 변환 적용하기
pandas에는 내장된 변환 도구가 아주 많지만, 때로는 더 고급스러운 로직이 필요할 때가 있어요. apply 함수는 사용자 정의 함수를 DataFrame의 행 또는 열에 적용할 수 있게 해 주어, 고급 변환과 피처 생성의 가능성을 열어 줍니다.
find_street_name() 함수는 "street_address"에서 거리 이름만 파싱하고 번지수를 제거합니다. 이 함수는 메모리에 로드되어 있으며, 이제 raw_testing_scores DataFrame에 적용할 준비가 되어 있어요.
이 연습은 강의의 일부입니다
Python으로 ETL과 ELT
연습 안내
transform()함수 정의 안에서find_street_name()함수를 사용해"street_name"라는 이름의 새 열을 생성하세요.transform()함수를 사용해raw_testing_scoresDataFrame을 정리하세요.- 새
"street_name"열을 확인할 수 있도록cleaned_testing_scoresDataFrame의 head를 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
def transform(raw_data):
# Use the apply function to extract the street_name from the street_address
raw_data["street_name"] = raw_data.____(
# Pass the correct function to the apply method
____,
axis=1
)
return raw_data
# Transform the raw_testing_scores DataFrame
cleaned_testing_scores = ____(raw_testing_scores)
# Print the head of the cleaned_testing_scores DataFrame
print(cleaned_testing_scores.____())