Skip to content

narwhals.Expr.cat

get_categories

get_categories() -> ExprT

Get unique categories from column.

Examples:

>>> import pandas as pd
>>> import narwhals as nw
>>> df_native = pd.DataFrame(
...     {"fruits": ["apple", "mango", "mango"]}, dtype="category"
... )
>>> df = nw.from_native(df_native)
>>> df.select(nw.col("fruits").cat.get_categories()).to_native()
  fruits
0  apple
1  mango