결측 데이터 처리하기

Python으로 하는 탐색적 데이터 분석

George Boorman

Curriculum Manager, DataCamp

결측치가 왜 문제인가요?

  • 분포에 영향
    • 키가 큰 학생의 데이터 누락
    • 특정 집단이 불균형적으로 대표됨, 예: 가장 나이가 많은 학생에 대한 데이터가 부족한 경우
  • 잘못된 결론 도출 가능

Sample versus population height distribution, showing the sample has a lower maximum and mean

Python으로 하는 탐색적 데이터 분석

데이터 직군 직무 데이터

설명 데이터 유형
Working_Year 데이터가 수집된 연도 Float
Designation 직함 String
Experience 경력 수준 예: "Mid", "Senior" String
Employment_Status 고용 계약 유형 예: "FT", "PT" String
Employee_Location 근무 국가 String
Company_Size 기업 규모 레이블 예: "S", "M", "L" String
Remote_Working_Ratio 원격 근무 비율 integer
Salary_USD 미국 달러 기준 급여 Float
Python으로 하는 탐색적 데이터 분석

경력 수준별 급여

Box plot of salaries by experience level using the clean dataset, with an upper limit near 600000 dollars

Box plot of salaries by experience level using a dataset with missing values, showing an upper limit near 450000 dollars

Python으로 하는 탐색적 데이터 분석

결측치 확인하기

print(salaries.isna().sum())
Working_Year            12
Designation             27
Experience              33
Employment_Status       31
Employee_Location       28
Company_Size            40
Remote_Working_Ratio    24
Salary_USD              60
dtype: int64
Python으로 하는 탐색적 데이터 분석

결측치 대응 전략

  • 결측값 삭제
    • 전체 값의 5% 이하일 때
  • 평균, 중앙값, 최빈값 대치
    • 분포와 맥락에 따라 다릅니다
  • 하위 그룹별로 대치
    • 경력 수준에 따라 연봉의 중앙값이 다릅니다
Python으로 하는 탐색적 데이터 분석

결측치 제거하기

threshold = len(salaries) * 0.05
print(threshold)
30
Python으로 하는 탐색적 데이터 분석

결측치 제거하기

cols_to_drop = salaries.columns[salaries.isna().sum() <= threshold]

print(cols_to_drop)
Index(['Working_Year', 'Designation', 'Employee_Location',
       'Remote_Working_Ratio'],
      dtype='object')
salaries.dropna(subset=cols_to_drop, inplace=True)
Python으로 하는 탐색적 데이터 분석

요약 통계로 대치하기

cols_with_missing_values = salaries.columns[salaries.isna().sum() > 0]
print(cols_with_missing_values)
Index(['Experience', 'Employment_Status', 'Company_Size', 'Salary_USD'], 
    dtype='object')
for col in cols_with_missing_values[:-1]:
    salaries[col].fillna(salaries[col].mode()[0])
Python으로 하는 탐색적 데이터 분석

남은 결측치 확인

print(salaries.isna().sum())
Working_Year             0
Designation              0
Experience               0
Employment_Status        0
Employee_Location        0
Company_Size             0
Remote_Working_Ratio     0
Salary_USD              41
Python으로 하는 탐색적 데이터 분석

하위 그룹별 대치

salaries_dict = salaries.groupby("Experience")["Salary_USD"].median().to_dict()

print(salaries_dict)
{'Entry': 55380.0, 'Executive': 135439.0, 'Mid': 74173.5, 'Senior': 128903.0}
Python으로 하는 탐색적 데이터 분석

하위 그룹별 대치

salaries["Salary_USD"] = salaries["Salary_USD"].fillna(salaries["Experience"].map(salaries_dict))
Python으로 하는 탐색적 데이터 분석

결측값 없음!

print(salaries.isna().sum())
Working_Year            0
Designation             0
Experience              0
Employment_Status       0
Employee_Location       0
Company_Size            0
Remote_Working_Ratio    0
Salary_USD              0
dtype: int64
Python으로 하는 탐색적 데이터 분석

연습해 봅시다!

Python으로 하는 탐색적 데이터 분석

Preparing Video For Download...