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

后端 未结 7 1106
隐瞒了意图╮
隐瞒了意图╮ 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:43

    Use df.select(cols.head, cols.tail: _*)

    Let me know if it works :)

    Explanation from @Ben:

    The key is the method signature of select:

    select(col: String, cols: String*)
    

    The cols:String* entry takes a variable number of arguments. :_* unpacks arguments so that they can be handled by this argument. Very similar to unpacking in python with *args. See here and here for other examples.

    0 讨论(0)
提交回复
热议问题