所以我有給定的資料框:
-------------------- -------------
| entity_id| state|
-------------------- -------------
| ha_tdeg| 39.9|
| memory_free| 1459.4|
| srv_tdeg| 39.0|
| as_tempera...| 9.5|
| as_humidity| 81.71|
| as_pressure| 1003.35|
| as_am_humidity| 22.16|
| as_pm_humidity| 4.64|
| memory_free| 1460.0|
| ha_tdeg| 38.0|
| memory_free| 1459.3|
-------------------- -------------
我試圖在“entity_id”包含“濕度”的每個“狀態”中添加一個百分號。因此,如下面的代碼所示,我在使用它之前將“狀態”列設定為“字串”。但是每當我執行下面的命令并嘗試連接 '%' (或任何其他字串)時,所有值都變為“null”。對我來說有趣的是,如果我嘗試連接一個包裝為字串(“10”)的數字,它會執行數學加法。
克服這個問題的方法是什么?
這是我使用的代碼:
var humidityDF = df.filter(forExport("entity_id").contains("humidity") && df("state").isNotNull)
humidityDF = humidityDF.withColumn("state", humidityDF("state").cast("String"))
humidityDF = humidityDF.withColumn("state", col("state") "%")
我試過了 :
濕度DF = 濕度DF.withColumn("state", col("state").toString "%")
但這不起作用,因為 'withColumn' 只接受 Column 型別引數。
uj5u.com熱心網友回復:
import org.apache.spark.sql.functions.{lit, concat}
humidityDF = humidityDF.withColumn("state", concat(col("state"),lit("%")))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/447330.html
標籤:数据框 斯卡拉 阿帕奇火花 apache-spark-sql 铸件
