簡體   English   中英

Scala Spark:根據浮點數列中的值過濾行

[英]Scala Spark: Filter rows based on values in a column of Floats

為什么以下代碼不起作用? 我正在嘗試過濾掉行,使它們包含以下值: [10.0, 100.0]. Both of the following solutions produce the same result. Do I need to [10.0, 100.0]. Both of the following solutions produce the same result. Do I need to [10.0, 100.0]. Both of the following solutions produce the same result. Do I need to Cast()` 還是什么?

解決方案1:

dff1.select("hrs").filter(col("hrs").geq(lit("10")) && 
                          col("hrs").leq(lit("100")) ).show(10, truncate = false)

解決方案2:

dff1.select("hrs").filter(col("hrs") >= lit("10") && 
                          col("hrs") <= lit("100") ).show(10, truncate = false)

結果:

+------------------+
|hrs               |
+------------------+
|239.78444444444443|
|24.459444444444443|
|238.05944444444444|
|45.05138888888889 |
|213.6225          |
|20.04388888888889 |
|201.45333333333335|
|4393.384166666667 |
|260.2611111111111 |
|47.83083333333333 |
+------------------+

lit對於整數或浮點數不是必需的:

dff1.select("hrs").filter(col("hrs") >= 10 && col("hrs") <= 100)

也應該工作。

最好使用過濾器的表達式。 該表達式與您在 SQL“where”子句中使用的表達式相同(保留整數/浮點數並將字符串常量用單引號括起來)。

所以你的轉變變成了。

dff1.select("hrs").filter(" hrs >= 10 and hrs <= 100 ")

暫無
暫無

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

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