【问题标题】:Cannot convert column into bool [duplicate]无法将列转换为布尔值 [重复]
【发布时间】:2023-03-09 11:36:01
【问题描述】:

我正在尝试使用 when 更新 PySpark Dataframe 中的列。我正在使用数组检查多个条件。我收到错误'Cannot convert column into bool'。 我假设这是因为我使用in 来检查值列表。该错误为 and (&) 和 or (|) 等其他事物提供了替代方案,但 in 没有。 有什么方法可以检查所有值而不是链接几个 when 语句?代码如下

affirm = ['yes', 'y', 'Y', 'Yes', 'YES']
neg = ['no', 'n', 'N', 'No', 'NO']
new_df.withColumn('resp', when(col("resp") in affirm, 'Yes').when(col("resp") in neg, 'No').otherwise('null'))

【问题讨论】:

    标签: apache-spark pyspark apache-spark-sql


    【解决方案1】:

    试试这个:

    new_df.withColumn('resp', when(col("resp").isin(*affirm), 'Yes').when(col("resp").isin(*neg), 'No').otherwise('null'))
    

    See docs.

    【讨论】:

    • 感谢这工作!只是想知道,肯定前面的*是做什么的?
    • @greenteam *** 用于解包列表和字典。你可以阅读更多here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    相关资源
    最近更新 更多