유효하지 않은 행 제거하기
주석 처리된 행을 성공적으로 제거한 지금, 데이터의 일반적인 형식에 대한 정보를 받았어요. DataFrame에는 최소한 탭으로 구분된 5개의 열이 있어야 해요. 원래 DataFrame에는 단일 열만 있었다는 점을 기억하시고, 탭(\t) 문자로 데이터를 분할해야 해요.
주석 처리된 행이 제거된 DataFrame annotations_df가 이미 준비되어 있어요. spark.sql.functions 라이브러리는 F라는 별칭으로 사용할 수 있어요. DataFrame의 초기 행 개수는 변수 initial_count에 저장되어 있어요.
이 연습은 강의의 일부입니다
PySpark로 데이터 정제하기
연습 안내
annotations_dfDataFrame의'_c0'컬럼을 탭 문자로 분할해 새 변수tmp_fields를 생성하세요.- 앞 단계에서 정의한 필드 개수를 나타내는
'colcount'라는 새 컬럼을annotations_df에 추가하세요. - 필드가 5개 미만인 행을
annotations_df에서 필터링하여 제거하세요. - DataFrame의 행 수를 세고
initial_count와 비교하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Split _c0 on the tab character and store the list in a variable
tmp_fields = ____(annotations_df['_c0'], ____)
# Create the colcount column on the DataFrame
annotations_df = annotations_df.____('____', ____(____))
# Remove any rows containing fewer than 5 fields
annotations_df_filtered = annotations_df.____(~ (____))
# Count the number of rows
final_count = ____
print("Initial count: %d\nFinal count: %d" % (initial_count, final_count))