DataFrame 연결

Python으로 데이터 정제하기

Adel Nehme

VP of AI Curriculum, DataCamp

레코드 연계

Python으로 데이터 정제하기

레코드 연계

Python으로 데이터 정제하기

우리 DataFrame

census_A

             given_name  surname date_of_birth         suburb state  address_1
rec_id                                                                
rec-1070-org   michaela  neumann      19151111  winston hills   nsw  stanley street 
rec-1016-org   courtney  painter      19161214      richlands   vic  pinkerton circuit 
...

census_B

               given_name  surname date_of_birth             suburb state  address_1
rec_id                                                                      
rec-561-dup-0       elton      NaN      19651013         windermere   vic  light setreet 
rec-2642-dup-0   mitchell    maxon      19390212         north ryde   nsw  edkins street 
...
Python으로 데이터 정제하기

이미 수행한 작업

# recordlinkage 임포트 및 전체 페어 생성
import recordlinkage
indexer = recordlinkage.Index()
indexer.block('state')
full_pairs = indexer.index(census_A, census_B)

# 비교 단계 compare_cl = recordlinkage.Compare() compare_cl.exact('date_of_birth', 'date_of_birth', label='date_of_birth') compare_cl.exact('state', 'state', label='state') compare_cl.string('surname', 'surname', threshold=0.85, label='surname') compare_cl.string('address_1', 'address_1', threshold=0.85, label='address_1')
potential_matches = compare_cl.compute(full_pairs, census_A, census_B)
Python으로 데이터 정제하기

현재 진행 내용

Python으로 데이터 정제하기

잠재적 매칭

potential_matches

Python으로 데이터 정제하기

잠재적 매칭

potential_matches

Python으로 데이터 정제하기

잠재적 매칭

potential_matches

Python으로 데이터 정제하기

잠재적 매칭

potential_matches

Python으로 데이터 정제하기

유력 매칭

matches = potential_matches[potential_matches.sum(axis = 1) >= 3]
print(matches)

Python으로 데이터 정제하기

유력 매칭

matches = potential_matches[potential_matches.sum(axis = 1) >= 3]
print(matches)

Python으로 데이터 정제하기

인덱스 가져오기

matches.index
MultiIndex(levels=[['rec-1007-org', 'rec-1016-org', 'rec-1054-org', 'rec-1066-org', 
'rec-1070-org', 'rec-1075-org', 'rec-1080-org', 'rec-110-org', ...
# census_B에서만 인덱스 가져오기
duplicate_rows = matches.index.get_level_values(1)
print(census_B_index)
Index(['rec-2404-dup-0', 'rec-4178-dup-0', 'rec-1054-dup-0', 'rec-4663-dup-0',
       'rec-485-dup-0', 'rec-2950-dup-0', 'rec-1234-dup-0', ... , 'rec-299-dup-0'])
Python으로 데이터 정제하기

DataFrame 연결

# census_B에서 중복 찾기
census_B_duplicates = census_B[census_B.index.isin(duplicate_rows)]

# census_B에서 신규 행 찾기 census_B_new = census_B[~census_B.index.isin(duplicate_rows)]
# DataFrame 연결!
full_census = pd.concat([census_A, census_B_new])
Python으로 데이터 정제하기
# recordlinkage 임포트, 페어 생성, 열별 비교
...
# 잠재적 매칭 생성
potential_matches = compare_cl.compute(full_pairs, census_A, census_B)

# 3개 이상 열이 일치하는 매칭만 분리 matches = potential_matches[potential_matches.sum(axis = 1) >= 3]
# census_B의 매칭 인덱스만 가져오기 duplicate_rows = matches.index.get_level_values(1)
# census_B에서 신규 행 찾기 census_B_new = census_B[~census_B.index.isin(duplicate_rows)]
# DataFrame 연결! full_census = pd.concat([census_A, census_B_new])
Python으로 데이터 정제하기

연습해 봅시다!

Python으로 데이터 정제하기

Preparing Video For Download...