我想知道是否有方法可以同时更改PySpark数据框的两个(或多个)列。现在我正在使用withColumn
,但我不知道这是否意味着该条件将被检查两次(对于大型数据帧,这对我来说太昂贵了)。这段代码基本上检查其他两列(对于同一行)中的值,并基于它将两列更改为无/空。PySpark数据框:根据条件同时更改两列
condition = is_special_id_udf(col("id"))) & should_hide_response_udf(col("response_created"))
new_df = df.withColumn(
"response_text",
when(condition, None)
.otherwise(col("response_text"))
)
new_df = df.withColumn(
"response_created",
when(condition, None)
.otherwise(col("response_created"))
)
请分享完整的代码和示例数据。你的代码是不可重现的。 – mtoto
你真的需要这些数据吗?代码工作正常,我只是想知道是否有更好的方法来做同样的事情。 – mfcabrera
您正在创建两个相同的列,是您的问题该怎么做? – mtoto