ข้อจำกัดด้านชนิดข้อมูล

การทำความสะอาดข้อมูลใน Python

Adel Nehme

VP of AI Curriculum, DataCamp

โครงร่างคอร์ส

dirty_data

การทำความสะอาดข้อมูลใน Python

โครงร่างคอร์ส

side effects

การทำความสะอาดข้อมูลใน Python

โครงร่างคอร์ส

clean_data

การทำความสะอาดข้อมูลใน Python

โครงร่างคอร์ส

clean_data

บทที่ 1 - ปัญหาข้อมูลที่พบบ่อย

การทำความสะอาดข้อมูลใน Python

ทำไมต้องทำความสะอาดข้อมูล?

ds_workflow

การทำความสะอาดข้อมูลใน Python

ทำไมต้องทำความสะอาดข้อมูล?

ds_workflow

การทำความสะอาดข้อมูลใน Python

ทำไมต้องทำความสะอาดข้อมูล?

                                                                                   Garbage in Garbage out

การทำความสะอาดข้อมูลใน Python

ข้อจำกัดด้านชนิดข้อมูล

ชนิดข้อมูล ตัวอย่าง
ข้อความ ชื่อ, นามสกุล, ที่อยู่ ...
จำนวนเต็ม จำนวนผู้ติดตาม, จำนวนสินค้าที่ขาย ...
ทศนิยม อุณหภูมิ, อัตราแลกเปลี่ยน ...
ไบนารี สถานะสมรส, ลูกค้าใหม่, ใช่/ไม่ใช่, ...
วันที่ วันสั่งซื้อ, วันจัดส่ง ...
หมวดหมู่ สถานภาพสมรส, เพศ ...
ชนิดข้อมูลใน Python
str
int
float
bool
datetime
category
การทำความสะอาดข้อมูลใน Python

แปลง String เป็น Integer

# 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
การทำความสะอาดข้อมูลใน Python

แปลง String เป็น Integer

# 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
การทำความสะอาดข้อมูลใน Python

แปลง String เป็น Integer

# 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'
การทำความสะอาดข้อมูลใน Python

คำสั่ง 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:
การทำความสะอาดข้อมูลใน Python

ตัวเลขหรือหมวดหมู่?

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

0 = ไม่เคยแต่งงาน       1 = แต่งงานแล้ว       2 = แยกกันอยู่       3 = หย่าร้าง

df['marriage_status'].describe()
       marriage_status
...
mean              1.4
std               0.20
min               0.00
50%               1.8 ...
การทำความสะอาดข้อมูลใน Python

ตัวเลขหรือหมวดหมู่?

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

df.describe()
        marriage_status
count                 241
unique                4
top                   1
freq                  120
การทำความสะอาดข้อมูลใน Python

มาฝึกกันเถอะ!

การทำความสะอาดข้อมูลใน Python

Preparing Video For Download...