great_tables
  • Get Started
  • Examples
  • Reference
  • Blog

On this page

  • Parameters
  • Examples

from_column

from_column(column, na_value=None, fn=None)

Specify that a style value should be fetched from a column in the data.

Parameters

column : str

A column name in the data containing the styling information.

na_value : Any | None = None

A single value to replace any NA values in the column (currently not supported).

fn : Callable[[Any], Any] | None = None

A callable applied to transform each value extracted from column=.

Examples

This example demonstrates styling the "x" column.

Style the text color using the "color" column:

import pandas as pd
import polars as pl
from great_tables import GT, from_column, loc, style, px

df = pd.DataFrame({"x": [15, 20], "color": ["red", "blue"]})

(GT(df).tab_style(style=style.text(color=from_column("color")), locations=loc.body(columns=["x"])))
x color
15 red
20 blue

With polars, you can pass expressions directly:

df_polars = pl.from_pandas(df)

(
    GT(df_polars).tab_style(
        style=style.text(color=pl.col("color")), locations=loc.body(columns=["x"])
    )
)
x color
15 red
20 blue

Style the text size using values from the "x" column, with the px() helper function as the fn= parameter:

(
    GT(df).tab_style(
        style=style.text(color=from_column("color"), size=from_column("x", fn=px)),
        locations=loc.body(columns=["x"]),
    )
)
x color
15 red
20 blue