Upacking a list to select multiple columns from a spark data frame

后端 未结 7 1129
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 14:03

I have a spark data frame df. Is there a way of sub selecting a few columns using a list of these columns?

scala> df.columns
res0: Array[Stri         


        
7条回答
  •  轮回少年
    2020-12-07 14:41

    You can pass arguments of type Column* to select:

    val df = spark.read.json("example.json")
    val cols: List[String] = List("a", "b")
    //convert string to Column
    val col: List[Column] = cols.map(df(_))
    df.select(col:_*)
    

提交回复
热议问题