Lucrul cu coloane de tip array
Funcția SQL udf este disponibilă, la fel și un DataFrame df_before de tipul DataFrame[doc: array<string>, in: array<string>, out: array<string>].
Variabila TRIVIAL_TOKENS este un set. Conține anumite cuvinte pe care dorim să le eliminăm.
Acest exercițiu face parte din cursul
Introducere în Spark SQL în Python
Instrucțiuni pentru exercițiu
- Afișează rândurile din
df_beforeîn caredocconține elementul5. - Creează un UDF care elimină din coloana de tip array elementele prezente în
TRIVIAL_TOKENS. Ordinea elementelor nu trebuie păstrată. - Elimină tokenii din coloanele
inșioutdindf2care apar înTRIVIAL_TOKENS.
Exercițiu interactiv practic
Încearcă acest exercițiu completând acest cod de exemplu.
# 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()