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

2024-09-28 22:25:05 发布

您现在位置:Python中文网/ 问答频道 /正文

使用df.withColomn{}和otherwise(***empty_array***)
时,如何添加空数组 新的列类型是来自UDF的T.ArrayType(T.StringType())

我希望避免以NaN值结束


Tags: 类型df数组nanarrayemptyudfstringtype
2条回答

只需使用array(lit(None))

df.select(when(col('target_bool')=='true',array(lit(1))).otherwise(array(lit(None)))).show()

尝试以下操作-创建一个无值列并强制转换为数组()

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)

相关问题 更多 >