user5251470
user5251470

Reputation: 1

rearrange order of spark columns

I have a spark dataframe with many columns. Using Spark and Scala, I would like to select the columns in a specified order, but I don't want to hardcode the desired order. In pseudo-code, I'd like do something like:

val colNames = df.columns

val newOrder = colNames(colNames.length) ++ colNames(0:colNames.length-1)

df.select(newOrder)

How can I do this? Thanks!

Upvotes: 0

Views: 3192

Answers (1)

akuiper
akuiper

Reputation: 215137

You can do something like this:

val df = Seq((1,2,3)).toDF("A","B","C")

df.select(df.columns.last, df.columns.dropRight(1): _*).show

+---+---+---+
|  C|  A|  B|
+---+---+---+
|  3|  1|  2|
+---+---+---+

Upvotes: 1

Related Questions