练习数组列
已提供 SQL 函数 udf,以及一个名为 df_before 的 dataframe,类型为 DataFrame[doc: array<string>, in: array<string>, out: array<string>]。
变量 TRIVIAL_TOKENS 是一个 set。它包含我们想要移除的某些词。
本练习是课程的一部分
Python 中的 Spark SQL 入门
练习说明
- 显示
df_before中doc含有元素5的行。 - 创建一个 udf,用于从数组列中移除出现在
TRIVIAL_TOKENS里的元素。顺序无需保留。 - 在
df2中,从in和out列移除出现在TRIVIAL_TOKENS的标记。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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()