CommencezCommencez gratuitement

Pratique avec une colonne de tableaux

La fonction SQL udf est disponible, de même qu'un dataframe df_before, de type DataFrame[doc: array<string>, in: array<string>, out: array<string>].

La variable TRIVIAL_TOKENS est un ensemble. Elle contient certains mots que nous voulons retirer.

Cette activité fait partie du cours

Introduction à Spark SQL en Python

Voir le cours

Instructions de l’exercice

  • Affichez les lignes de df_beforedoc contient l'élément 5.
  • Créez une udf qui enlève les éléments de TRIVIAL_TOKENS d'une colonne de type tableau. L'ordre n'a pas à être conservé.
  • Retirez, dans df2, les jetons des colonnes in et out qui apparaissent dans TRIVIAL_TOKENS.

Exercice interactif pratique

Essayez cet exercice en complétant ce code d’exemple.

# 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()
Modifier et exécuter le code