Latihan kolom array
Fungsi SQL udf tersedia, dan sebuah dataframe df_before juga tersedia, dengan tipe DataFrame[doc: array<string>, in: array<string>, out: array<string>].
Variabel TRIVIAL_TOKENS adalah sebuah himpunan (set). Variabel ini berisi kata-kata tertentu yang ingin kita hapus.
Latihan ini adalah bagian dari kursus
Pengantar Spark SQL dalam Python
Petunjuk latihan
- Tampilkan baris dari
df_beforedi manadocmemuat item5. - Buat sebuah udf yang menghapus item dalam
TRIVIAL_TOKENSdari kolom array. Urutan tidak perlu dipertahankan. - Hapus token dari kolom
indanoutdidf2yang muncul dalamTRIVIAL_TOKENS.
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
# 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()