gt-extras
  • Intro
  • API Reference
  1. Utilities
  2. gt_merge_stack
  • 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
  1. Utilities
  2. gt_merge_stack

gt_merge_stack

gt_merge_stack(
    gt,
    col1,
    col2,
    font_size_main=14,
    font_size_secondary=10,
    font_weight_main='bold',
    font_weight_secondary='normal',
    color_main='black',
    color_secondary='grey',
    small_caps=True,
)

Merge two columns into a stacked format within a GT object.

The gt_merge_stack() function combines two columns in a GT object into a single column with a stacked format. The top section displays values from the first column (col1), and the bottom section displays values from the second column (col2). Both sections can be styled independently with customizable font sizes, weights, colors, and text variants.

The resulting table will hide col2, and the orignal col1 will contain the merged entries.

Parameters

gt : GT

A GT object to modify.

col1 : SelectExpr

The column containing values to display in the top section of the stack.

col2 : SelectExpr

The column containing values to display in the bottom section of the stack.

font_size_main : int = 14

The font size for the top section of the stack.

font_size_secondary : int = 10

The font size for the bottom section of the stack.

font_weight_main : Literal['normal', 'bold', 'bolder', 'lighter'] | int = 'bold'

The font weight for the top section of the stack. Options include "normal", "bold", "bolder", "lighter", or an integer value.

font_weight_secondary : Literal['normal', 'bold', 'bolder', 'lighter'] | int = 'normal'

The font weight for the bottom section of the stack.

color_main : str = 'black'

The text color for the top section of the stack.

color_secondary : str = 'grey'

The text color for the bottom section of the stack.

small_caps : bool = True

A boolean indicating whether the top section should use small caps styling.

Returns

: GT

A GT object with the merged and styled column.

Examples

import pandas as pd
from great_tables import GT
import gt_extras as gte

df = pd.read_csv("../assets/teams_colors_logos.csv")
df = (df.filter(items=["team_nick", "team_abbr", "team_conf", "team_division", "team_wordmark"]).head(8))

gt = GT(df, groupname_col="team_conf", rowname_col="team_nick")
gt = gt.fmt_image(columns="team_wordmark")


gt.pipe(
    gte.gt_merge_stack,
    col1="team_nick",
    col2="team_division",
)
team_abbr team_wordmark
NFC
Cardinals
NFC West
ARI
Falcons
NFC South
ATL
Panthers
NFC South
CAR
Bears
NFC North
CHI
AFC
Ravens
AFC North
BAL
Bills
AFC East
BUF
Bengals
AFC North
CIN
Browns
AFC North
CLE
gt_duplicate_column
gt_two_column_layout