How do I use multiple conditions with pyspark.sql.functions.when()?

前端 未结 3 1454
逝去的感伤
逝去的感伤 2020-12-14 16:22

I have a dataframe with a few columns. Now I want to derive a new column from 2 other columns:

from pyspark.sql import functions as F
new_df = df.withColumn         


        
相关标签:
3条回答
  • 2020-12-14 16:42

    when in pyspark multiple conditions can be built using &(for and) and | (for or), it is important to enclose every expressions within parenthesis that combine to form the condition

    %pyspark
    dataDF = spark.createDataFrame([(66, "a", "4"), 
                                    (67, "a", "0"), 
                                    (70, "b", "4"), 
                                    (71, "d", "4")],
                                    ("id", "code", "amt"))
    dataDF.withColumn("new_column",
           when((col("code") == "a") | (col("code") == "d"), "A")
          .when((col("code") == "b") & (col("amt") == "4"), "B")
          .otherwise("A1")).show()
    

    when in spark scala can be used with && and || operator to build multiple conditions

    //Scala
    val dataDF = Seq(
              (66, "a", "4"), (67, "a", "0"), (70, "b", "4"), (71, "d", "4"
              )).toDF("id", "code", "amt")
        dataDF.withColumn("new_column",
               when(col("code") === "a" || col("code") === "d", "A")
              .when(col("code") === "b" && col("amt") === "4", "B")
              .otherwise("A1"))
              .show()
    

    Output:

    +---+----+---+----------+
    | id|code|amt|new_column|
    +---+----+---+----------+
    | 66|   a|  4|         A|
    | 67|   a|  0|         A|
    | 70|   b|  4|         B|
    | 71|   d|  4|         A|
    +---+----+---+----------+
    
    0 讨论(0)
  • 2020-12-14 16:49

    you can also use from pyspark.sql.functions import col F.when(col("col-1")>0.0) & (col("col-2")>0.0), 1).otherwise(0)

    0 讨论(0)
  • 2020-12-14 16:56

    Use parentheses to enforce the desired operator precedence:

    F.when( (df["col-1"]>0.0) & (df["col-2"]>0.0), 1).otherwise(0)
    
    0 讨论(0)
提交回复
热议问题