데이터 타입 제약

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으로 데이터 정제하기

문자열을 정수로 변환

# 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으로 데이터 정제하기

문자열을 정수로 변환

# 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으로 데이터 정제하기

문자열을 정수로 변환

# 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으로 데이터 정제하기

Ayo berlatih!

Python으로 데이터 정제하기

Preparing Video For Download...