Cannot resolve column (numeric column name) in Spark Dataframe

后端 未结 2 1067
离开以前
离开以前 2020-12-29 11:33

This is my data:

scala> data.printSchema
root
 |-- 1.0: string (nullable = true)
 |-- 2.0: string (nullable = true)
 |-- 3.0: string (nullable = true)


        
相关标签:
2条回答
  • 2020-12-29 11:59

    You can use backticks to escape the dot, which is reserved for accessing columns for struct type:

    data.select("`2.0`").show
    +---+
    |2.0|
    +---+
    | , |
    +---+
    
    0 讨论(0)
  • 2020-12-29 12:07

    The problem is you can not add dot character in the column name while selecting from dataframe. You can have a look at this question, kind of similar.

    val data = spark.createDataFrame(Seq(
      ("Hello", ", ", "World!")
    )).toDF("1.0", "2.0", "3.0")
    data.select(sanitize("2.0")).show
    
    def sanitize(input: String): String = s"`$input`"
    
    0 讨论(0)
提交回复
热议问题