Insert spanners by splitting column names with a delimiter.
This generates one or more spanners (and sets column labels), by splitting the column name by the specified delimiter text (delim) and placing the fragments from top to bottom (i.e., higher-level spanners to the column labels) or vice versa.
For example, the three side-by-side column names rating_1, rating_2, and rating_3 will by default produce a spanner labeled “rating” above columns labeled “1”, “2”, and “3”.
Parameters
delim:str='.'
Delimiter for splitting, default to ".".
columns:SelectExpr=None
The columns to target. Can either be a single column name or a series of column names provided in a list.
split:Literal['first', 'last']='last'
Should the delimiter splitting occur from the “last” instance of the delim character or from the “first”? The default here uses the “last” keyword, and splitting begins at the last instance of the delimiter in the column name. This option only has some consequence when there is a limit value applied that is lesser than the number of delimiter characters for a given column name (i.e., number of splits is not the maximum possible number).
limit:int=-1
Limit for splitting. An optional limit to place on the splitting procedure. The default -1 means that a column name will be split as many times are there are delimiter characters. In other words, the default means there is no limit. If an integer value is given to limit then splitting will cease at the iteration given by limit. This works in tandem with split since we can adjust the number of splits from either the right side (split = “last”) or left side (split = “first”) of the column name.
reverse:bool=False
Should the order of split names be reversed? By default, this is False.
The GT object is returned. This is the same object that the method is called on so that we can facilitate method chaining.
Examples
Let’s create a table table that includes the column names province.NL_ZH.pop, province.NL_ZH.gdp, province.NL_NH.pop, and province.NL_NH.gdp, we can see that we have a naming system that has a well-defined structure. We start with the more general to the left (“province”) and move to the more specific on the right (“pop”). If the columns are in the table in this exact order, then things are in an ideal state as the eventual spanner labels will form from this neighboring. When using tab_spanner_delim() here with delim set as “.” we get the following table: