Pyspark使用一个when语句更新两个列



所以我在PySpark中使用df.Withcolumn()来创建一个列,并使用F.when()来指定何时应该更新列的标准。

df = df.withColumn('ab', F.when(df['text']=="0", 1).otherwise(0))

基本上,如果它符合标准,我将更新列为'1'。现在,我想更新同一df中的另一列,如果相同的标准匹配(例如。df['text']=="0")。是否有任何方法在PySpark更新两列使用一个when语句?

不可能。你只能创建struct:

>>> from pyspark.sql.functions import *
>>> df.withColumn('ab', F.when(df['text']=="0" , struct(1, "foo")).otherwise(struct(0, "bar")))

最新更新