모두 연결하기!
이전 레슨에서 restaurants와 restaurants_new를 연결하는 작업의 대부분을 마무리했어요. 잠재적으로 일치할 수 있는 행 쌍을 만들고, cuisine_type과 city 열은 정확히 일치하는지를 찾았으며, rest_name 열은 유사한 문자열인지 비교했습니다. 점수가 담긴 DataFrame은 potential_matches에 저장해 두었죠.
이제 두 DataFrame을 실제로 연결할 차례입니다. 먼저 potential_matches에서 앞서 언급한 열들 기준으로 일치하는 restaurants_new의 모든 행 인덱스를 추출하세요. 그런 다음 해당 인덱스를 기준으로 restaurants_new를 서브셋팅하고, 마지막으로 중복되지 않는 값들을 restaurants와 이어 붙이면 됩니다. 모든 DataFrame은 환경에 준비되어 있으며, pandas는 pd로 임포트되어 있어요.
이 연습은 강의의 일부입니다
Python으로 데이터 정제하기
연습 안내
.sum()메서드를 사용해 행 합계가 3 이상인potential_matches의 행만 분리하세요..get_level_values()메서드로matches의 두 번째 열 인덱스를 추출하세요. 이는restaurants_new에서 일치하는 레코드의 행 인덱스를 의미합니다.matching_indices에 없는 행만 남기도록restaurants_new를 서브셋팅하세요.restaurants와non_dup를 이어 붙이세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Isolate potential matches with row sum >=3
matches = ____[____.___(____) >= ____]
# Get values of second column index of matches
matching_indices = matches.____.____(____)
# Subset restaurants_new based on non-duplicate values
non_dup = ____[~restaurants_new.index.____(____)]
# Concatenate restaurants and non_dup
full_restaurants = pd.____([____, ____])
print(full_restaurants)