我正在努力(在pyspark/spark内)。具体而言,数据帧如下所示:
ID response_variable id_name id_1 NULL "randomidDKHFD" id_2 NULL NULL id_3 NULL "randomid86438"
我希望能够转换数据帧,以便无论何时id_name有一个非空条目,response_变量都变为1。因此,在本例中:
ID response_variable in_data id_1 1 "randomidDKHFD" id_2 NULL NULL id_3 1 "randomid86438"
有人知道怎么做吗?
你可以用 withColumn when
withColumn
when
蟒蛇:
yourDf.withColumn("response_variable", when(col("id_name").isNotNull(), lit(1)).otherwise(col("response_variable")))
yourDf.withColumn("response_variable", when(col("id_name").isNotNull, lit(1)).otherwise(col("response_variable")))