使用时如何添加空数组df.withColomn when() 以及 otherwise(***empty_array***) 新列类型为 T.ArrayType(T.StringType()) 来自自定义项我想避免以nan值结束。
when()
otherwise(***empty_array***)
T.ArrayType(T.StringType())
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)
wtzytmuj2#
简单使用 array(lit(None)) ```df.select(when(col('target_bool')=='true',array(lit(1))).otherwise(array(lit(None)))).show()
array(lit(None))
2条答案
按热度按时间ssgvzors1#
在下面尝试-创建一个无值的列并强制转换为array()
wtzytmuj2#
简单使用
array(lit(None))
```df.select(when(col('target_bool')=='true',array(lit(1))).otherwise(array(lit(None)))).show()