Practicar con columnas array
La función SQL udf está disponible, así como un dataframe df_before de tipo DataFrame[doc: array<string>, in: array<string>, out: array<string>].
La variable TRIVIAL_TOKENS es un conjunto. Contiene ciertas palabras que queremos eliminar.
Este ejercicio forma parte del curso
Introducción a Spark SQL en Python
Instrucciones del ejercicio
- Muestra las filas de
df_beforedondedoccontiene el elemento5. - Crea una udf que elimine los elementos de
TRIVIAL_TOKENSde una columna de tipo array. No es necesario preservar el orden. - Elimina de las columnas
inyoutendf2los tokens que aparezcan enTRIVIAL_TOKENS.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
# 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()