범주형 변수

Python으로 데이터 정제하기

Adel Nehme

Content Developer @DataCamp

어떤 오류가 있을 수 있나요?

I) 값 불일치

  • 불일치 필드: 'married', 'Maried', 'UNMARRIED', 'not married'
  • 뒤따르는 공백: 'married ', ' married '

II) 너무 많은 범주를 소수로 축소

  • 새 그룹 생성: 연속형 가계소득에서 0-20K, 20-40K 등 범주 생성
  • 그룹 재매핑: 가계소득 범주를 'rich', 'poor' 두 개로 매핑

III) 데이터 타입을 category로 지정(1장 참고)

Python으로 데이터 정제하기

값 일관성

대소문자 혼용: 'married', 'Married', 'UNMARRIED', 'unmarried'

# 결혼 상태 열 가져오기
marriage_status = demographics['marriage_status']
marriage_status.value_counts()
unmarried    352
married      268
MARRIED      204
UNMARRIED    176
dtype: int64
Python으로 데이터 정제하기

값 일관성

# DataFrame에서 값 개수 계산
marriage_status.groupby('marriage_status').count()
                 household_income  gender
marriage_status                          
MARRIED                       204     204
UNMARRIED                     176     176
married                       268     268
unmarried                     352     352
Python으로 데이터 정제하기

값 일관성

# 대문자화

marriage_status['marriage_status'] = marriage_status['marriage_status'].str.upper() marriage_status['marriage_status'].value_counts()
UNMARRIED    528
MARRIED      472
# 소문자화

marriage_status['marriage_status'] = marriage_status['marriage_status'].str.lower() marriage_status['marriage_status'].value_counts()
unmarried    528
married      472
Python으로 데이터 정제하기

값 일관성

뒤따르는 공백: 'married ', 'married', 'unmarried', ' unmarried'

# 결혼 상태 열 가져오기
marriage_status = demographics['marriage_status']
marriage_status.value_counts()
 unmarried   352
unmarried    268
married      204
married      176
dtype: int64
Python으로 데이터 정제하기

값 일관성

# 모든 공백 제거
demographics = demographics['marriage_status'].str.strip()
demographics['marriage_status'].value_counts()
unmarried    528
married      472
Python으로 데이터 정제하기

데이터를 범주로 묶기

데이터로 범주 만들기: income 열에서 income_group 열 생성.

# 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)
# income_group 열 출력
demographics[['income_group', 'household_income']]
     category  household_income
0   200K-500K  189243
1       500K+  778533
..
Python으로 데이터 정제하기

데이터를 범주로 묶기

데이터로 범주 만들기: income 열에서 income_group 열 생성.

# cut() 사용 - 범위와 이름 정의
ranges = [0,200000,500000,np.inf]
group_names = ['0-200K', '200K-500K', '500K+']
# 소득 그룹 열 생성
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
Python으로 데이터 정제하기

데이터를 범주로 묶기

범주 통합: 범주형 열의 값을 더 적은 범주로 축소.

operating_system 열 현재: 'Microsoft', 'MacOS', 'IOS', 'Android', 'Linux'

operating_system 열 목표: 'DesktopOS', 'MobileOS'

# 매핑 딕셔너리 생성 후 치환
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)
Python으로 데이터 정제하기

Vamos praticar!

Python으로 데이터 정제하기

Preparing Video For Download...