Scala Spark DataFrame : dataFrame.select multiple columns given a Sequence of column names

后端 未结 2 1258
半阙折子戏
半阙折子戏 2020-12-08 02:51
val columnName=Seq(\"col1\",\"col2\",.....\"coln\");

Is there a way to do dataframe.select operation to get dataframe containing only the column na

相关标签:
2条回答
  • 2020-12-08 03:05

    Since dataFrame.select() expects a sequence of columns and we have a sequence of strings, we need to convert our sequence to a List of cols and convert that list to the sequence. columnName.map(name => col(name)): _* gives a sequence of columns from a sequence of strings, and this can be passed as a parameter to select():

      val columnName = Seq("col1", "col2")
      val DFFiltered = DF.select(columnName.map(name => col(name)): _*)
    
    0 讨论(0)
  • 2020-12-08 03:20
    val columnNames = Seq("col1","col2",....."coln")
    
    // using the string column names:
    val result = dataframe.select(columnNames.head, columnNames.tail: _*)
    
    // or, equivalently, using Column objects:
    val result = dataframe.select(columnNames.map(c => col(c)): _*)
    
    0 讨论(0)
提交回复
热议问题