Ràng buộc kiểu dữ liệu

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

Adel Nehme

VP of AI Curriculum, DataCamp

Tổng quan khóa học

dữ liệu bẩn

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

Tổng quan khóa học

tác dụng phụ

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

Tổng quan khóa học

dữ liệu sạch

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

Tổng quan khóa học

dữ liệu sạch

Chương 1 - Lỗi dữ liệu thường gặp

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

Vì sao cần làm sạch dữ liệu?

quy trình khoa học dữ liệu

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

Vì sao cần làm sạch dữ liệu?

quy trình khoa học dữ liệu

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

Vì sao cần làm sạch dữ liệu?

                                                                                   Rác vào, rác ra

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

Ràng buộc kiểu dữ liệu

Kiểu dữ liệu Ví dụ
Văn bản Tên, họ, địa chỉ ...
Số nguyên # thuê bao, # sản phẩm bán ...
Số thập phân Nhiệt độ, tỷ giá $ ...
Nhị phân Đã kết hôn, khách mới, có/không ...
Ngày Ngày đặt hàng, ngày giao ...
Phân loại Tình trạng hôn nhân, giới tính ...
Kiểu dữ liệu Python
str
int
float
bool
datetime
category
Làm sạch dữ liệu với Python

Chuỗi thành số nguyên

# Import CSV file and output header
sales = pd.read_csv('sales.csv')
sales.head(2)
   SalesOrderID    Revenue    Quantity
0         43659     23153$          12
1         43660      1457$           2
# Get data types of columns
sales.dtypes
SalesOrderID    int64
Revenue         object
Quantity        int64
dtype: object
Làm sạch dữ liệu với Python

Chuỗi thành số nguyên

# Get DataFrame information
sales.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 31465 entries, 0 to 31464
Data columns (total 3 columns):
SalesOrderID     31465 non-null int64
Revenue          31465 non-null object
Quantity         31465 non-null int64
dtypes: int64(2), object(1)
memory usage: 737.5+ KB
Làm sạch dữ liệu với Python

Chuỗi thành số nguyên

# Print sum of all Revenue column
sales['Revenue'].sum()
'23153$1457$36865$32474$472$27510$16158$5694$6876$40487$807$6893$9153$6895$4216..
# Remove $ from Revenue column
sales['Revenue'] = sales['Revenue'].str.strip('$')
sales['Revenue'] = sales['Revenue'].astype('int')
# Verify that Revenue is now an integer
assert sales['Revenue'].dtype == 'int'
Làm sạch dữ liệu với Python

Câu lệnh assert

# This will pass
assert 1+1 == 2
# This will not pass
assert 1+1 == 3
AssertionError                            Traceback (most recent call last)
         assert 1+1 == 3
AssertionError:
Làm sạch dữ liệu với Python

Số hay phân loại?

...   marriage_status    ...
...                 3    ...
...                 1    ...
...                 2    ...

0 = Chưa từng kết hôn       1 = Đã kết hôn       2 = Ly thân       3 = Ly hôn

df['marriage_status'].describe()
       marriage_status
...
mean              1.4
std               0.20
min               0.00
50%               1.8 ...
Làm sạch dữ liệu với Python

Số hay phân loại?

# Convert to categorical
df["marriage_status"] = df["marriage_status"].astype('category')

df.describe()
        marriage_status
count                 241
unique                4
top                   1
freq                  120
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...