S’exercer avec une colonne de type array
La fonction SQL udf est disponible, ainsi qu’un dataframe df_before de type DataFrame[doc: array<string>, in: array<string>, out: array<string>].
La variable TRIVIAL_TOKENS est un ensemble (set). Elle contient certains mots que nous souhaitons supprimer.
Cet exercice fait partie du cours
Introduction à Spark SQL en Python
Instructions
- Affichez les lignes de
df_beforeoùdoccontient l’élément5. - Créez une UDF qui supprime, dans une colonne de type array, les éléments présents dans
TRIVIAL_TOKENS. L’ordre n’a pas besoin d’être conservé. - Supprimez, dans
df2, les jetons des colonnesinetoutqui apparaissent dansTRIVIAL_TOKENS.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# 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()