Liên kết DataFrame

Làm sạch dữ liệu với Python

Adel Nehme

VP of AI Curriculum, DataCamp

Liên kết bản ghi

Làm sạch dữ liệu với Python

Liên kết bản ghi

Làm sạch dữ liệu với Python

Các DataFrame của chúng ta

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 
...
Làm sạch dữ liệu với Python

Những gì đã làm

# Import recordlinkage và tạo tất cả cặp
import recordlinkage
indexer = recordlinkage.Index()
indexer.block('state')
full_pairs = indexer.index(census_A, census_B)

# Bước so sánh 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)
Làm sạch dữ liệu với Python

Việc đang thực hiện

Làm sạch dữ liệu với Python

Các ghép nối tiềm năng

potential_matches

Làm sạch dữ liệu với Python

Các ghép nối tiềm năng

potential_matches

Làm sạch dữ liệu với Python

Các ghép nối tiềm năng

potential_matches

Làm sạch dữ liệu với Python

Các ghép nối tiềm năng

potential_matches

Làm sạch dữ liệu với Python

Khớp có khả năng cao

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

Làm sạch dữ liệu với Python

Khớp có khả năng cao

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

Làm sạch dữ liệu với Python

Lấy chỉ mục

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', ...
# Chỉ lấy chỉ mục từ 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'])
Làm sạch dữ liệu với Python

Liên kết DataFrame

# Tìm bản trùng trong census_B
census_B_duplicates = census_B[census_B.index.isin(duplicate_rows)]

# Tìm hàng mới trong census_B census_B_new = census_B[~census_B.index.isin(duplicate_rows)]
# Liên kết các DataFrame
full_census = pd.concat([census_A, census_B_new])
Làm sạch dữ liệu với Python
# Import recordlinkage, tạo cặp và so sánh theo cột
...
# Tạo các ghép nối tiềm năng
potential_matches = compare_cl.compute(full_pairs, census_A, census_B)

# Lọc khớp có từ 3 cột trùng trở lên matches = potential_matches[potential_matches.sum(axis = 1) >= 3]
# Lấy chỉ mục chỉ cho các hàng khớp của census_B duplicate_rows = matches.index.get_level_values(1)
# Tìm hàng mới trong census_B census_B_new = census_B[~census_B.index.isin(duplicate_rows)]
# Liên kết các DataFrame full_census = pd.concat([census_A, census_B_new])
Làm sạch dữ liệu với Python

Ayo berlatih!

Làm sạch dữ liệu với Python

Preparing Video For Download...