【问题标题】:PySpark assign names to a column values 'withcolumn'PySpark 将名称分配给列值“withcolumn”
【发布时间】:2019-12-20 07:30:06
【问题描述】:

我是 PySaprk 的新手,但对 R 有一些经验。

问题:我想为 ONE 列中列出的高度(数字)指定一个名称。我开始编写如下代码:

w = Window.partitionBy("student_id")
df_enc_hw = df_enc_hw.withColumn("stuname", \
                       when(lower(col("height")) <= 4, "under_ht") 
                      .when(lower(col("height")) > 4 < 5, "ok_ht")  
                      .when(lower(col("height")) >=5 < 6, "normal_ht")  
                      .when(lower(col("height")) >=6, "abnor_ht")) 

但是出现以下错误:

    633 
    634     def __nonzero__(self):
--> 635         raise ValueError("Cannot convert column into bool: please use '&' for 'and', '|' for 'or', "
    636                          "'~' for 'not' when building DataFrame boolean expressions.")
    637     __bool__ = __nonzero__

ValueError: Cannot convert column into bool: please use '&' for 'and', '|' for 'or', '~' for 'not' when building DataFrame boolean expressions.

感谢您的帮助 克

【问题讨论】:

  • lower(col("height")) &gt; 4 &lt; 5 更改为(lower(col("height")) &gt; 4) &amp; (lower(col("height")) &lt; 5)(其他条件相同)。这是运算符优先级的问题。

标签: python pyspark pyspark-sql pyspark-dataframes


【解决方案1】:

你应该把你的条件分解成单独的表达式,像这样:

df_enc_hw = df_enc_hw.withColumn("stuname", \
                       when(lower(col("height")) <= 4, "under_ht") 
                      .when((lower(col("height")) > 4) & (lower(col("height")) < 5), "ok_ht")  
                      .when((lower(col("height")) >=5) & (lower(col("height")) < 6), "normal_ht")  
                      .when(lower(col("height")) >=6, "abnor_ht"))

【讨论】:

  • 非常感谢您的宝贵时间和帮助。它正在工作:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-23
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
相关资源
最近更新 更多