Python으로 데이터 정제하기
Adel Nehme
Content Developer @DataCamp
미리 정해진 유한한 범주 집합
| 데이터 유형 | 예시 값 | 숫자 표현 |
|---|---|---|
| 혼인 상태 | unmarried, married |
0,1 |
| 가구 소득 구간 | 0-20K, 20-40K, ... |
0,1, .. |
| 대출 상태 | default,payed,no_loan |
0,1,2 |
혼인 상태는 반드시 unmarried _또는_ married 중 하나입니다


# 연구 데이터 읽기 및 출력
study_data = pd.read_csv('study.csv')
study_data
name birthday blood_type
1 Beth 2019-10-20 B-
2 Ignatius 2020-07-08 A-
3 Paul 2019-08-12 O+
4 Helen 2019-03-17 O-
5 Jennifer 2019-12-17 Z+
6 Kennedy 2020-04-27 A+
7 Keith 2019-04-19 AB+
# 가능한 혈액형 목록
categories
blood_type
1 O-
2 O+
3 A-
4 A+
5 B+
6 B-
7 AB+
8 AB-
# 연구 데이터 읽기 및 출력
study_data = pd.read_csv('study.csv')
study_data
name birthday blood_type
1 Beth 2019-10-20 B-
2 Ignatius 2020-07-08 A-
3 Paul 2019-08-12 O+
4 Helen 2019-03-17 O-
5 Jennifer 2019-12-17 Z+ <--
6 Kennedy 2020-04-27 A+
7 Keith 2019-04-19 AB+
# 가능한 혈액형 목록
categories
blood_type
1 O-
2 O+
3 A-
4 A+
5 B+
6 B-
7 AB+
8 AB-



inconsistent_categories = set(study_data['blood_type']).difference(categories['blood_type'])
print(inconsistent_categories)
{'Z+'}
# 일치하지 않는 범주가 있는 행 가져오기 inconsistent_rows = study_data['blood_type'].isin(inconsistent_categories)study_data[inconsistent_rows]
name birthday blood_type
5 Jennifer 2019-12-17 Z+
inconsistent_categories = set(study_data['blood_type']).difference(categories['blood_type']) inconsistent_rows = study_data['blood_type'].isin(inconsistent_categories) inconsistent_data = study_data[inconsistent_rows]# 불일치 범주 제거 후 일치 데이터만 얻기 consistent_data = study_data[~inconsistent_rows]
name birthday blood_type
1 Beth 2019-10-20 B-
2 Ignatius 2020-07-08 A-
3 Paul 2019-08-12 O+
4 Helen 2019-03-17 O-
... ... ... ...
Python으로 데이터 정제하기