How to change a column position in a spark dataframe?

后端 未结 6 1038
无人共我
无人共我 2020-12-08 19:33

I was wondering if it is possible to change the position of a column in a dataframe, actually to change the schema?

Precisely if I have got a dataframe like [f

6条回答
  •  感情败类
    2020-12-08 19:50

    Like others have commented, I'm curious to know why would you do this as the order is not relevant when you can query the columns by their names.

    Anyway, using a select should give the feeling the columns have moved in schema description:

    val data = Seq(
      ("a",       "hello", 1),
      ("b",       "spark", 2)
    )
    .toDF("field1", "field2", "field3")
    
    data
     .show()
    
    data
     .select("field3", "field2", "field1")
     .show()
    

提交回复
热议问题