결측 비율 계산하기
자동화는 데이터 과학의 미래예요. 데이터 준비 작업 일부를 자동화할 수 있으면 큰 이점을 얻습니다. 이번 연습에서는 결측치가 특정 임계값을 넘는 열을 자동으로 삭제해 보겠습니다.
이 연습은 강의의 일부입니다
PySpark로 하는 Feature Engineering
연습 안내
df데이터프레임과 0과 1 사이의 실수threshold를 매개변수로 받는 함수column_dropper()를 정의하세요.where(),isNull(),count()를 사용해 결측값의 비율을 계산하세요.- 결측 비율이 임계값보다 큰지 확인하고, 크다면
drop()을 사용해 해당 열을 삭제하세요. - 임계값을 0.6으로 설정해
df에 대해column_dropper()를 실행하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
def column_dropper(df, threshold):
# Takes a dataframe and threshold for missing values. Returns a dataframe.
total_records = df.____()
for col in df.columns:
# Calculate the percentage of missing values
missing = df.____(df[col].____()).____()
missing_percent = ____ / ____
# Drop column if percent of missing is more than threshold
if ____ > ____:
df = df.____(col)
return df
# Drop columns that are more than 60% missing
df = ____(____, ____)