We can use a list of strings or integers to select columns by name or position, respectively.
gt_ex.cols_move_to_start(columns=["date", 1, -1])
date
char
time
num
fctr
2015-01-15
apricot
13:35
0.1111
one
2015-02-15
banana
14:40
2.222
two
2015-03-15
coconut
15:45
33.33
three
2015-04-15
durian
16:50
444.4
four
Note the code above moved the following columns:
The string "date" matched the column of the same name.
The integer 1 matched the second column (this is similar to list indexing).
The integer -1 matched the last column.
Moreover, the order of the list defines the order of selected columns. In this case, "data" was the first entry, so it’s the very first column in the new table.
Using Polars selectors
When using a Polars DataFrame, you can select columns using Polars selectors. The example below uses Polars selectors to move all columns that start with "c" or "f" to the start of the table.
import polars as plimport polars.selectors as cspl_df = pl.from_pandas(lil_exibble)GT(pl_df).cols_move_to_start(columns=cs.starts_with("c") | cs.starts_with("f"))
char
fctr
num
date
time
apricot
one
0.1111
2015-01-15
13:35
banana
two
2.222
2015-02-15
14:40
coconut
three
33.33
2015-03-15
15:45
durian
four
444.4
2015-04-15
16:50
In general, selection should match the behaviors of the PolarsDataFrame.select() method.