pyspark 选择列内容长度 < x 的行

pyspark selecting rows where column content length < x

我有一个 pyspark 数据框,其中一列的内容是字符串类型。我只想 select 该列上的字符串长度大于 5 的行。我尝试使用 size 函数,但它只适用于数组。

from pyspark.sql.functions import col, explode, regexp_replace, size

new_df = df.select("col_1", explode(col("col_2")) \
    .select("col_1", "col_2") \
    .where(col("col_1").isNotNull()) \
    .where(size(col("col_2")) <= 5) \
    .distinct()

有什么方法可以在不使用 UDF 的情况下按列内容长度 select 吗?

如前所述。您可以使用 length。 所以你的例子应该是这样的:

from pyspark.sql.functions import col, explode, regexp_replace, length

new_df = df.select("col_1", explode(col("col_2")) \
    .select("col_1", "col_2") \
    .where(col("col_1").isNotNull()) \
    .where(length(col("col_2")) <= 5) \
    .distinct()