great_tables
  • Get Started
  • Examples
  • Reference
  • Blog

On this page

  • Parameters
  • Returns
  • Examples

GT.cols_move_to_start

GT.cols_move_to_start(self, columns)

Move one or more columns to the start.

We can easily move set of columns to the beginning of the column series and we only need to specify which columns. It’s possible to do this upstream of Great Tables, however, it is easier with this method and it presents less possibility for error. The ordering of the columns that are moved to the start is preserved (same with the ordering of all other columns in the table).

The columns supplied in columns must all exist in the table. If you need to place one or columns at the end of the column series, the cols_move_to_end() method should be used. More control is offered with the cols_move() method, where columns could be placed after a specific column.

Parameters

columns : SelectExpr

The columns to target. Can either be a single column name or a series of column names provided in a list.

Returns

: GT

The GT object is returned. This is the same object that the method is called on so that we can facilitate method chaining.

Examples

For this example, we’ll use a portion of the countrypops dataset to create a simple table. Let’s move the year column, which is the middle column, to the start of the column series with the cols_move_to_start() method.

from great_tables import GT
from great_tables.data import countrypops

countrypops_mini = countrypops.loc[countrypops["country_name"] == "Fiji"][
    ["country_name", "year", "population"]
].tail(5)

GT(countrypops_mini).cols_move_to_start(columns="year")
year country_name population
2018 Fiji 918996
2019 Fiji 918465
2020 Fiji 920422
2021 Fiji 924610
2022 Fiji 929766

We can also move multiple columns at a time. With the same countrypops-based table (countrypops_mini), let’s move both the year and population columns to the start of the column series.

GT(countrypops_mini).cols_move_to_start(columns=["year", "population"])
year population country_name
2018 918996 Fiji
2019 918465 Fiji
2020 920422 Fiji
2021 924610 Fiji
2022 929766 Fiji