gt-extras
  • Intro
  • API Reference
  1. Utilities
  2. gt_duplicate_column
  • API Reference
  • Plotting
    • gt_plt_bar
    • gt_plt_bar_pct
    • gt_plt_bar_stack
    • gt_plt_conf_int
    • gt_plt_dot
    • gt_plt_dumbbell
    • gt_plt_winloss
  • Colors
    • gt_color_box
    • gt_data_color_by_group
    • gt_highlight_cols
    • gt_highlight_rows
    • gt_hulk_col_numeric
  • Themes
    • gt_theme_538
    • gt_theme_dark
    • gt_theme_dot_matrix
    • gt_theme_espn
    • gt_theme_excel
    • gt_theme_guardian
    • gt_theme_nytimes
    • gt_theme_pff
  • Icons and Images
    • add_text_img
    • fa_icon_repeat
    • gt_fa_rank_change
    • gt_fa_rating
    • img_header
  • Utilities
    • fmt_pct_extra
    • gt_add_divider
    • gt_duplicate_column
    • gt_merge_stack
    • gt_two_column_layout
    • with_hyperlink
    • with_tooltip

On this page

  • Parameters
  • Returns
  • Examples
  • Note
  1. Utilities
  2. gt_duplicate_column

gt_duplicate_column

gt_duplicate_column(gt, column, after=None, append_text='_dupe', dupe_name=None)

Duplicate a column in a GT object.

The gt_duplicate_column() function takes an existing GT object and creates a duplicate (without styling) of the specified column. The duplicated column can be renamed using either dupe_name or by appending text to the original column name, and positioned at a specific location in the table.

Parameters

gt : GT

A GT object to modify.

column : SelectExpr

The column to duplicate. Can be a column name or index.

after : str | None = None

The column after which to place the duplicated column. If None, the duplicated column will be moved to the end of the table.

append_text : str | None = '_dupe'

Text to append to the original column name for the duplicate. Only used if dupe_name is not provided. Defaults to "_dupe".

dupe_name : str | None = None

The name for the duplicated column. If provided, this overrides append_text.

Returns

: GT

A GT object with the duplicated column added.

Examples

from great_tables import GT
from great_tables.data import gtcars
import gt_extras as gte

gtcars_mini = gtcars[["mfr", "model", "year", "hp"]].head(5)
gt = GT(gtcars_mini)

# Duplicate with custom name and position
gt.pipe(
    gte.gt_duplicate_column,
    column="hp",
    after="year",
)
mfr model year hp_dupe hp
Ford GT 2017 647.0 647.0
Ferrari 458 Speciale 2015 597.0 597.0
Ferrari 458 Spider 2015 562.0 562.0
Ferrari 458 Italia 2014 562.0 562.0
Ferrari 488 GTB 2016 661.0 661.0

Note

Styles generated by GT.tab_style() will not be duplicated. This may be accommodated in the future.

gt_add_divider
gt_merge_stack