Luyện tập với cột mảng
Hàm SQL udf đã sẵn có, và một dataframe df_before cũng đã sẵn có, kiểu DataFrame[doc: array<string>, in: array<string>, out: array<string>].
Biến TRIVIAL_TOKENS là một set. Nó chứa một số từ mà chúng ta muốn loại bỏ.
Bài tập này là một phần của khóa học
Nhập môn Spark SQL bằng Python
Hướng dẫn bài tập
- Hiển thị các dòng của
df_beforenơidocchứa phần tử5. - Tạo một udf để loại bỏ các phần tử trong
TRIVIAL_TOKENSkhỏi một cột mảng. Không cần giữ nguyên thứ tự. - Loại bỏ các token khỏi các cột
invàouttrongdf2nếu chúng xuất hiện trongTRIVIAL_TOKENS.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
# 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()