ComenzarEmpieza gratis

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

Ver curso

Instrucciones del ejercicio

  • Muestra las filas de df_before donde doc contiene el elemento 5.
  • Crea una udf que elimine los elementos de TRIVIAL_TOKENS de una columna de tipo array. No es necesario preservar el orden.
  • Elimina de las columnas in y out en df2 los tokens que aparezcan en TRIVIAL_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()
Editar y ejecutar código