reorder
Changes the order of the selected columns. reorder selects the columns; finish it with by, byDesc or byName to get a new DataFrame.
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:
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:
byDesc puts them in descending order:
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:
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:
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.
reorderColumnsBy
Reorders all columns of the dataframe by the value of columnExpression.
Parameters:
atAnyDepth— iftrue, also reorder the columns inside everyColumnGroup, each within its own group. Iffalse, only the top-level columns are reordered.desc— apply descending orderinFrameColumns— iftrue, also reorder the columns inside every dataframe of aFrameColumn, each dataframe on its own and with the sameatAnyDepth. Iffalse, they keep their order: likecolsAtAnyDepth(),atAnyDepthdoes 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:
With atAnyDepth = false, the columns inside name keep their order:
reorderColumnsByName
Reorders all columns of the dataframe in lexicographic order of their names. It is a shortcut for reorderColumnsBy { name() }.
Parameters:
atAnyDepth— iftrue, also reorder the columns inside everyColumnGroup, each within its own group. Iffalse, only the top-level columns are reordered.desc— apply descending orderinFrameColumns— iftrue, also reorder the columns inside every dataframe of aFrameColumn, each dataframe on its own and with the sameatAnyDepth. Iffalse, 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:
With atAnyDepth = false, the columns inside name keep their order: