Biến phân loại

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

Adel Nehme

Content Developer @DataCamp

Có thể gặp lỗi nào?

I) Không nhất quán giá trị

  • Trường không nhất quán: 'married', 'Maried', 'UNMARRIED', 'not married'..
  • Khoảng trắng cuối: 'married ', ' married '..

II) Gộp quá nhiều hạng mục thành ít

  • Tạo nhóm mới: hạng mục 0-20K, 20-40K ... từ dữ liệu thu nhập hộ liên tục
  • Ánh xạ nhóm sang nhóm mới: ánh xạ hạng mục thu nhập hộ thành 2 nhóm 'rich', 'poor'

III) Đảm bảo kiểu dữ liệu là category (đã xem ở Chương 1)

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

Nhất quán giá trị

Chữ hoa/thường: 'married', 'Married', 'UNMARRIED', 'unmarried'..

# Lấy cột tình trạng hôn nhân
marriage_status = demographics['marriage_status']
marriage_status.value_counts()
unmarried    352
married      268
MARRIED      204
UNMARRIED    176
dtype: int64
Làm sạch dữ liệu với Python

Nhất quán giá trị

# Đếm giá trị theo nhóm trong DataFrame
marriage_status.groupby('marriage_status').count()
                 household_income  gender
marriage_status                          
MARRIED                       204     204
UNMARRIED                     176     176
married                       268     268
unmarried                     352     352
Làm sạch dữ liệu với Python

Nhất quán giá trị

# Chuyển thành chữ hoa

marriage_status['marriage_status'] = marriage_status['marriage_status'].str.upper() marriage_status['marriage_status'].value_counts()
UNMARRIED    528
MARRIED      472
# Chuyển thành chữ thường

marriage_status['marriage_status'] = marriage_status['marriage_status'].str.lower() marriage_status['marriage_status'].value_counts()
unmarried    528
married      472
Làm sạch dữ liệu với Python

Nhất quán giá trị

Khoảng trắng thừa: 'married ', 'married', 'unmarried', ' unmarried'..

# Lấy cột tình trạng hôn nhân
marriage_status = demographics['marriage_status']
marriage_status.value_counts()
 unmarried   352
unmarried    268
married      204
married      176
dtype: int64
Làm sạch dữ liệu với Python

Nhất quán giá trị

# Loại bỏ mọi khoảng trắng
demographics = demographics['marriage_status'].str.strip()
demographics['marriage_status'].value_counts()
unmarried    528
married      472
Làm sạch dữ liệu với Python

Gộp dữ liệu thành nhóm

Tạo nhóm từ dữ liệu: cột income_group từ cột income.

# Dùng qcut()
import pandas as pd
group_names = ['0-200K', '200K-500K', '500K+']
demographics['income_group'] = pd.qcut(demographics['household_income'], q = 3, 
                                       labels = group_names)
# In cột income_group
demographics[['income_group', 'household_income']]
     category  household_income
0   200K-500K  189243
1       500K+  778533
..
Làm sạch dữ liệu với Python

Gộp dữ liệu thành nhóm

Tạo nhóm từ dữ liệu: cột income_group từ cột income.

# Dùng cut() - tạo khoảng và tên nhóm
ranges = [0,200000,500000,np.inf]
group_names = ['0-200K', '200K-500K', '500K+']
# Tạo cột nhóm thu nhập
demographics['income_group'] = pd.cut(demographics['household_income'], bins=ranges, 
                                      labels=group_names)
demographics[['income_group', 'household_income']]
     category  Income
0      0-200K  189243
1       500K+  778533
Làm sạch dữ liệu với Python

Gộp dữ liệu thành nhóm

Gộp nhiều hạng mục thành ít hơn: giảm số hạng mục trong cột phân loại.

Cột operating_system hiện: 'Microsoft', 'MacOS', 'IOS', 'Android', 'Linux'

Cột operating_system cần thành: 'DesktopOS', 'MobileOS'

# Tạo từ điển ánh xạ và thay thế
mapping = {'Microsoft':'DesktopOS', 'MacOS':'DesktopOS', 'Linux':'DesktopOS',
           'IOS':'MobileOS', 'Android':'MobileOS'}
devices['operating_system'] = devices['operating_system'].replace(mapping)
devices['operating_system'].unique()
array(['DesktopOS', 'MobileOS'], dtype=object)
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...