簡體   English   中英

Pyspark dataframe withColumn 'when' 不使用 '<' 或 '>'

[英]Pyspark dataframe withColumn 'when' not working with '<' or '>'

我正在嘗試使用 'when' 在相同的 dataframe 中創建一個大於或小於條件的新列,如下所示:

df = df.withColumn(
    "new_col",
    when(col("age") < 17, 1234) #when(col("DAYS") < 30, lit("ECONOMICAL"))
    .when(col("age") > 17, 5678)
    .otherwise(df.old_col)

但是,我收到此錯誤消息:“Row”和“int”實例之間不支持“<”

我試過這個when(int(col("age")) < 17, 1234)但它沒有用。

我也試過使用 '<=' 和 '>=' 但它們也沒有用

我什至在這里看到另一篇帖子建議用戶在像我這樣的 withColumn 中包含when(col("DAYS") < 30, lit("ECONOMICAL"))的解決方案,但我也嘗試在結果中lit但效果不佳.

有誰知道為什么這不能完成? 我在 rows 和 int 之間做 == 沒有問題,問題是當我嘗試 '<' 和 '>' 時?

找到解決方案 - 需要添加顯式轉換...

df = df.withColumn(
    "new_col",
    when(col("age").cast("int") < 17, 1234)
    .when(col("age").cast("int") > 17, 5678)
    .otherwise(df.old_col)

該解決方案有效。

暫無
暫無

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

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