DataFrame에 고급 변환 적용하기
pandas에는 다양한 내장 변환 도구가 있지만, 때로는 변환 작업에 더 고급 로직이 필요한 경우가 있습니다. apply 함수를 사용하면 DataFrame의 행이나 열에 사용자 정의 함수를 적용할 수 있어, 고급 변환과 특성 생성이 가능해집니다.
find_street_name() 함수는 "street_address"에서 거리 이름(street name)을 파싱하여 문자열에서 번지수를 제거합니다. 이 함수는 이미 메모리에 로드되어 있으며, raw_testing_scores DataFrame에 적용할 준비가 되어 있습니다.
이 연습은 강의의 일부입니다
Python으로 하는 ETL과 ELT
연습 안내
transform()함수의 정의에서find_street_name()함수를 사용하여"street_name"이라는 새 열을 생성하세요.transform()함수를 사용하여raw_testing_scoresDataFrame을 정리하세요.cleaned_testing_scoresDataFrame의 처음 몇 개 행을 출력하여 새로 생긴"street_name"열을 확인하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
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.____())