pyspark df.withColumn 具有三个条件

pyspark df.withColumn with three conditions

我有两列代表 'TeamName''MatchResult' 例如:

ManCity    L
Liverpool  D
Arsenal    W

我正在尝试根据不同足球队的比赛结果创建代表 'Points' 的第三列。所以赢3分,平1分,输0分

我已经尝试使用 when 和 if 函数 .withColumn,但无法正确 syntax

非常感谢您的宝贵时间

ManCity    L    0
Liverpool  D    1
Arsenal    W    3

您可以使用:

from pyspark.sql.functions import when, col

df = df.withColumn("points", when(col("MatchResult") == "W", 3).when(col("MatchResult") == "D", 1).otherwise(0))