배열 컬럼 연습
SQL 함수 udf를 사용할 수 있으며, DataFrame[doc: array<string>, in: array<string>, out: array<string>] 타입의 데이터프레임 df_before도 제공됩니다.
변수 TRIVIAL_TOKENS는 set입니다. 제거하려는 특정 단어들이 들어 있습니다.
이 연습은 강의의 일부입니다
Python에서 Spark SQL 입문
연습 안내
doc에 항목5가 포함된df_before의 행을 보여주세요.- 배열 컬럼에서
TRIVIAL_TOKENS에 있는 항목들을 제거하는 udf를 만드세요. 순서는 유지하지 않아도 됩니다. df2의in과out컬럼에서TRIVIAL_TOKENS에 포함된 토큰을 제거하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Show the rows where doc contains the item '5'
df_before.where(array_contains('doc', '____')).show()
# UDF removes items in TRIVIAL_TOKENS from array
rm_trivial_udf = udf(lambda x:
list(set(x) - ____) if x
else x,
ArrayType(____()))
# Remove trivial tokens from 'in' and 'out' columns of df2
df_after = df_before.withColumn('in', ____('in'))\
.withColumn('out', ____('out'))
# Show the rows of df_after where doc contains the item '5'
df_after.where(array_contains('doc','5')).show()