【问题标题】:How to use when and Otherwise statement for a Spark dataframe by boolean columns?如何通过布尔列对 Spark 数据框使用 when 和 Otherwise 语句?
【发布时间】:2023-01-09 18:08:16
【问题描述】:

我有一个包含三列的数据集,col 1:country(String),col 2:threshold_1(bool),col 3:threshold_2(bool)

我正在尝试使用此逻辑创建一个新列,但出现错误

我正在为此使用 Palantir 代码工作簿,谁能告诉我我在这里缺少什么?

df = df.withColumn("Threshold_Filter", 
        when(df["country"]=="INDIA" & df["threshold_1"]==True | df["threshold_2 "]==True, "Ind_country"
     ).otherwise("Dif_country"))

【问题讨论】:

    标签: apache-spark pyspark palantir-foundry foundry-code-repositories foundry-code-workbooks


    【解决方案1】:

    你只需要把你的陈述放在括号里。

    df = (
        df
        .withColumn(
            "Threshold_Filter",
            when(
                (df["country"]=="INDIA") & 
                (df["threshold_1"]==True) | 
                (df["threshold_2 "]==True), 
                "Ind_country")
            .otherwise("Dif_country"))
    )
    

    【讨论】:

      猜你喜欢
      • 2015-11-04
      • 2018-04-10
      • 1970-01-01
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      • 2016-12-15
      • 2020-12-27
      相关资源
      最近更新 更多