Skip to content

narwhals.Expr.name

keep

keep() -> ExprT

Keep the original root name of the expression.

Notes

For Polars versions prior to 1.32, this will undo any previous renaming operations on the expression.

Examples:

>>> import pandas as pd
>>> import narwhals as nw
>>> df_native = pd.DataFrame({"foo": [1, 2], "BAR": [4, 5]})
>>> df = nw.from_native(df_native)
>>> df.select(nw.col("foo").alias("alias_for_foo").name.keep()).columns
['foo']

map

map(function: Callable[[str], str]) -> ExprT

Rename the output of an expression by mapping a function over the root name.

Parameters:

Name Type Description Default
function Callable[[str], str]

Function that maps a root name to a new name.

required
Notes

For Polars versions prior to 1.32, this will undo any previous renaming operations on the expression.

Examples:

>>> import pandas as pd
>>> import narwhals as nw
>>> df_native = pd.DataFrame({"foo": [1, 2], "BAR": [4, 5]})
>>> df = nw.from_native(df_native)
>>> renaming_func = lambda s: s[::-1]  # reverse column name
>>> df.select(nw.col("foo", "BAR").name.map(renaming_func)).columns
['oof', 'RAB']

prefix

prefix(prefix: str) -> ExprT

Add a prefix to the root column name of the expression.

Parameters:

Name Type Description Default
prefix str

Prefix to add to the root column name.

required
Notes

For Polars versions prior to 1.32, this will undo any previous renaming operations on the expression.

Examples:

>>> import polars as pl
>>> import narwhals as nw
>>> df_native = pl.DataFrame({"foo": [1, 2], "BAR": [4, 5]})
>>> df = nw.from_native(df_native)
>>> df.select(nw.col("foo", "BAR").name.prefix("with_prefix")).columns
['with_prefixfoo', 'with_prefixBAR']

suffix

suffix(suffix: str) -> ExprT

Add a suffix to the root column name of the expression.

Parameters:

Name Type Description Default
suffix str

Suffix to add to the root column name.

required
Notes

For Polars versions prior to 1.32, this will undo any previous renaming operations on the expression.

Examples:

>>> import polars as pl
>>> import narwhals as nw
>>> df_native = pl.DataFrame({"foo": [1, 2], "BAR": [4, 5]})
>>> df = nw.from_native(df_native)
>>> df.select(nw.col("foo", "BAR").name.suffix("_with_suffix")).columns
['foo_with_suffix', 'BAR_with_suffix']

to_lowercase

to_lowercase() -> ExprT

Make the root column name lowercase.

Notes

For Polars versions prior to 1.32, this will undo any previous renaming operations on the expression.

Examples:

>>> import pyarrow as pa
>>> import narwhals as nw
>>> df_native = pa.table({"foo": [1, 2], "BAR": [4, 5]})
>>> df = nw.from_native(df_native)
>>> df.select(nw.col("foo", "BAR").name.to_lowercase()).columns
['foo', 'bar']

to_uppercase

to_uppercase() -> ExprT

Make the root column name uppercase.

Notes

For Polars versions prior to 1.32, this will undo any previous renaming operations on the expression.

Examples:

>>> import pyarrow as pa
>>> import narwhals as nw
>>> df_native = pa.table({"foo": [1, 2], "BAR": [4, 5]})
>>> df = nw.from_native(df_native)
>>> df.select(nw.col("foo", "BAR").name.to_uppercase()).columns
['FOO', 'BAR']