DataFrame 1.0 Help

reorder

Changes the order of the selected columns. reorder selects the columns; finish it with by, byDesc or byName to get a new DataFrame.

reorder { columns } [.cast<ColumnType>() ] .by { columnExpression } | .byDesc { columnExpression } | .byName(desc = false) columnExpression: DataColumn.(DataColumn) -> Value

Related operations: Move / rename / reorder columns

Don't confuse reorder with sortBy, which changes the order of rows, not columns.

See column selectors for how to select the columns for this operation.

The examples on this page use the following dataframe:

df
df.reorder { age..isHappy }.byName()
df.reorder { "age".."isHappy" }.byName()

The selected columns change places only with each other: they take the positions of the selected columns, in the new order. Positions of other columns do not change. The values in the columns do not change either.

The columnExpression lambda is computed for every selected column. The column is passed to it both as the receiver (this) and as the argument (it).

by puts the columns in ascending order of the computed value:

val df = dataFrameOf("c", "d", "a", "b")( 3, 4, 1, 2, 1, 1, 1, 1, ) df.reorder("d", "b").cast<Int>().by { sum() }

byDesc puts them in descending order:

val df = dataFrameOf("c", "d", "a", "b")( 3, 4, 1, 2, 1, 1, 1, 1, ) df.reorder("a", "b").cast<Int>().byDesc { sum() }

Columns for which columnExpression gives equal results keep their original order. Here name and city have names of the same length, so name stays before city:

df.reorder { age and name and city }.byDesc { it.name().length }
df.reorder("age", "name", "city").byDesc { it.name().length }

If selected columns belong to different column groups, they are reordered within their groups, so no column moves to another group. Here age and city change places at the top level, and firstName and lastName change places inside name:

df.reorder { age and city and name.firstName and name.lastName }.byName(desc = true)
df.reorder { "age" and "city" and "name"["firstName"] and "name"["lastName"] }.byName(desc = true)

When exactly one ColumnGroup is selected, reordering is applied to its nested columns. A column group selected together with other columns moves as a whole, and its nested columns keep their order.

df.reorder { name }.byName(desc = true)
df.reorder("name").byName(desc = true)

reorderColumnsBy

Reorders all columns of the dataframe by the value of columnExpression.

reorderColumnsBy(atAnyDepth = true, desc = false, inFrameColumns = false) { columnExpression }

Parameters:

  • atAnyDepth — if true, also reorder the columns inside every ColumnGroup, each within its own group. If false, only the top-level columns are reordered.

  • desc — apply descending order

  • inFrameColumns — if true, also reorder the columns inside every dataframe of a FrameColumn, each dataframe on its own and with the same atAnyDepth. If false, they keep their order: like colsAtAnyDepth(), atAnyDepth does not look inside frame columns.

As in reorder, columns for which columnExpression gives equal results keep their original order. The examples use the dataframe from the top of the page. With the default atAnyDepth = true, the columns inside name are reordered too:

df.reorderColumnsBy { name().length }

With atAnyDepth = false, the columns inside name keep their order:

df.reorderColumnsBy(atAnyDepth = false) { name().length }

reorderColumnsByName

Reorders all columns of the dataframe in lexicographic order of their names. It is a shortcut for reorderColumnsBy { name() }.

reorderColumnsByName(atAnyDepth = true, desc = false, inFrameColumns = false)

Parameters:

  • atAnyDepth — if true, also reorder the columns inside every ColumnGroup, each within its own group. If false, only the top-level columns are reordered.

  • desc — apply descending order

  • inFrameColumns — if true, also reorder the columns inside every dataframe of a FrameColumn, each dataframe on its own and with the same atAnyDepth. If false, they keep their order.

The examples use the dataframe from the top of the page. With the default atAnyDepth = true, the columns inside name are reordered too:

df.reorderColumnsByName(desc = true)

With atAnyDepth = false, the columns inside name keep their order:

df.reorderColumnsByName(atAnyDepth = false, desc = true)
15 October 2026