How to change a column position in a spark dataframe?

后端 未结 6 1054
无人共我
无人共我 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:53

    You can get the column names, reorder them however you want, and then use select on the original DataFrame to get a new one with this new order:

    val columns: Array[String] = dataFrame.columns
    val reorderedColumnNames: Array[String] = ??? // do the reordering you want
    val result: DataFrame = dataFrame.select(reorderedColumnNames.head, reorderedColumnNames.tail: _*)
    

提交回复
热议问题