dl5txlt9  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(246)

我想替换 "BMU 1 Cell 1 Temp. (C)""BMU_1_Cell_1_Temp_C" 也可以把柱子铸成双柱。
尝试了以下选项:
以单引号提供列名

val df= df1.withColumn("`BMU 1 Cell 1 Temp. (C)`",col("`BMU 1 Cell 1 Temp. (C)`").cast("Double")).withColumnRenamed("`BMU 1 Cell 1 Temp. (C)`","BMU_1_Cell_1_Temp_C")
val df= df1.withColumn("BMU 1 Cell 1 Temp. (C)",col("BMU 1 Cell 1 Temp. (C)").cast("Double")).withColumnRenamed("BMU 1 Cell 1 Temp. (C)","BMU_1_Cell_1_Temp_C").replaceAll("\\.","_"))

获取以下错误:

org.apache.spark.sql.AnalysisException: cannot resolve '`BMU 1 PCB Temp. (C)`'

你能帮我做这个吗。

ne5o7dgx

ne5o7dgx1#

这是我的spark 3.0.0。测试。

val df = spark.createDataFrame(Seq(("1", "123.456"))).toDF("id", "BMU 1 Cell 1 Temp. (C)")

df.withColumnRenamed("BMU 1 Cell 1 Temp. (C)", "BMU_1_Cell_1_Temp_C")
  .withColumn("BMU_1_Cell_1_Temp_C", $"BMU_1_Cell_1_Temp_C".cast("double")).show

+---+-------------------+
| id|BMU_1_Cell_1_Temp_C|
+---+-------------------+
|  1|            123.456|
+---+-------------------+

这两个都管用。

val df = spark.createDataFrame(Seq(("1", "123.456"))).toDF("id", "BMU 1 Cell 1 Temp. (C)")

val cols = df.columns.map(c => c.replaceAll("([.] )|[ ]", "_").replaceAll("[()]", ""))

df.toDF(cols: _*).withColumn("BMU_1_Cell_1_Temp_C", $"BMU_1_Cell_1_Temp_C".cast("double")).show()

+---+-------------------+
| id|BMU_1_Cell_1_Temp_C|
+---+-------------------+
|  1|            123.456|
+---+-------------------+

相关问题