The cols_reorder() method allows you to completely rearrange the column order of a table. Provide all column names in the exact order you want them to appear. This is useful when you need full control over the column layout and want to express the entire ordering in a single call, rather than using multiple cols_move(), cols_move_to_start(), or cols_move_to_end() calls.
Every column in the table must appear exactly once in the columns= list. If any columns are missing or extra names are provided, a ValueError will be raised.
Parameters
columns:SelectExpr
A list of all column names in the desired display order. This can be a list of column name strings or a column selection expression (e.g., Polars selectors). All columns in the table must be included exactly once.
For tables with many columns, you can use Python’s iterable unpacking to build the column list programmatically. Here we use the full exibble dataset (9 columns) and move fctr to the front while pushing num and char to the end—without typing every column name in between:
# Unpack the first three column names and capture all remaining ones in `rest`# exibble.columns is: ["num", "char", "fctr", "date", "time", "datetime", "currency", "row", "group"]num, char, fctr, *rest = exibble.columns# Build the new order: fctr first, then all middle columns in their# original order, and finally char and num moved to the end( GT(exibble) .cols_reorder([fctr, *rest, char, num]))
fctr
date
time
datetime
currency
row
group
char
num
one
2015-01-15
13:35
2018-01-01 02:22
49.95
row_1
grp_a
apricot
0.1111
two
2015-02-15
14:40
2018-02-02 14:33
17.95
row_2
grp_a
banana
2.222
three
2015-03-15
15:45
2018-03-03 03:44
1.39
row_3
grp_a
coconut
33.33
four
2015-04-15
16:50
2018-04-04 15:55
65100.0
row_4
grp_a
durian
444.4
five
2015-05-15
17:55
2018-05-05 04:00
1325.81
row_5
grp_b
5550.0
six
2015-06-15
2018-06-06 16:11
13.255
row_6
grp_b
fig
seven
19:10
2018-07-07 05:22
row_7
grp_b
grapefruit
777000.0
eight
2015-08-15
20:20
0.44
row_8
grp_b
honeydew
8880000.0
This unpacking technique is especially handy for wide tables where you want to pin a few columns to the start or end without manually listing every column in between. The *rest variable automatically adapts if columns are added to or removed from the dataset, making your table code more resilient to upstream schema changes.