簡體   English   中英

pySpark withColumn 有兩個條件

[英]pySpark withColumn with two conditions

我想過濾兩個條件: clean_reference.Output == " "clean_reference.Primary == "DEFAULT" 如果兩個條件都適用,則clean_reference.Output否則為"NI"

下面的代碼不接受我的clean_reference.Output作為我的 when() 值。

final_reference = clean_reference.withColumn("Output",f.when(clean_reference.Output == " ")| (clean_reference.Primary == "DEFAULT"), clean_reference.Output).otherwise("NI")
TypeError: when() missing 1 required positional argument: 'value'

把你的 cols 像f.col()和 value 分配為f.lit()

final_reference = clean_reference.withColumn("Output",\
                       f.when((f.col("Output") == " ")|                              
                             (f.col("Primary") ==\
                              "DEFAULT"), f.col("Output"))\
                                             .otherwise(f.lit("NI")))

相同的代碼,只是固定了大括號。

final_reference = clean_reference.withColumn(
        "OutputItemNameByValue",
        f.when( 
          (clean_reference.OutputItemNameByValue == " ") | 
          (clean_reference.PrimaryLookupAttributeValue == "TRIANA_DEFAULT"),
          clean_reference.OutputItemNameByValue
        ).otherwise("Not Implemented")
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM