如何在仅包含空值和真值的列中应用 udf 函数
How to apply udf functions in a column which contain only null and true value
我有一个包含列名 "x" 和 "Y" 的数据框,y 列仅包含空值和真值,
x y
br null
br null
bm null
bm null
br true
bm true
我需要创建一个 udf,它会创建另一列 "z" 并将 null 标记为 1,将 true 标记为 0
x y z
br null 1
br null 1
bm null 1
bm null 1
br true 0
bm true 0
您实际上不需要为此使用 UDF。使用 when
和 otherwise
子句非常简单:
from pyspark.sql.functions import when, col
df.withColumn("z", when(col("y").isNull(), 1).otherwise(0))
我有一个包含列名 "x" 和 "Y" 的数据框,y 列仅包含空值和真值,
x y
br null
br null
bm null
bm null
br true
bm true
我需要创建一个 udf,它会创建另一列 "z" 并将 null 标记为 1,将 true 标记为 0
x y z
br null 1
br null 1
bm null 1
bm null 1
br true 0
bm true 0
您实际上不需要为此使用 UDF。使用 when
和 otherwise
子句非常简单:
from pyspark.sql.functions import when, col
df.withColumn("z", when(col("y").isNull(), 1).otherwise(0))