【发布时间】: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")) > 4 < 5更改为(lower(col("height")) > 4) & (lower(col("height")) < 5)(其他条件相同)。这是运算符优先级的问题。
标签: python pyspark pyspark-sql pyspark-dataframes