如何在pyspark中使用when和other添加空数组

v9tzhpje  于 2021-05-27  发布在  Spark
关注(0)|答案(2)|浏览(317)

使用时如何添加空数组
df.withColomn when() 以及 otherwise(***empty_array***) 新列类型为 T.ArrayType(T.StringType()) 来自自定义项
我想避免以nan值结束。

ssgvzors

ssgvzors1#

在下面尝试-创建一个无值的列并强制转换为array()

df_b = df_b.withColumn("empty_array", F.when(F.col("rn") == F.lit("1"), (None))).withColumn("empty_array", F.col("empty_array").cast(T.ArrayType(T.StringType())))
df_b.show()

 root
 |-- col1: string (nullable = true)
 |-- col2: string (nullable = true)
 |-- rn: integer (nullable = true)
 |-- case_condition: integer (nullable = true)
 |-- empty_array: array (nullable = true)
 |    |-- element: string (containsNull = true)
wtzytmuj

wtzytmuj2#

简单使用 array(lit(None)) ```
df.select(when(col('target_bool')=='true',array(lit(1))).otherwise(array(lit(None)))).show()

相关问题