Skip to content

narwhals.Expr.name

keep() -> ExprT

Keep the original root name of the expression.

Returns:

Type Description
ExprT

A new expression.

Notes

This will undo any previous renaming operations on the expression. Due to implementation constraints, this method can only be called as the last expression in a chain. Only one name operation per expression will work.

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(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

Returns:

Type Description
ExprT

A new expression.

Notes

This will undo any previous renaming operations on the expression. Due to implementation constraints, this method can only be called as the last expression in a chain. Only one name operation per expression will work.

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: 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

Returns:

Type Description
ExprT

A new expression.

Notes

This will undo any previous renaming operations on the expression. Due to implementation constraints, this method can only be called as the last expression in a chain. Only one name operation per expression will work.

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: 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

Returns:

Type Description
ExprT

A new expression.

Notes

This will undo any previous renaming operations on the expression. Due to implementation constraints, this method can only be called as the last expression in a chain. Only one name operation per expression will work.

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() -> ExprT

Make the root column name lowercase.

Returns:

Type Description
ExprT

A new expression.

Notes

This will undo any previous renaming operations on the expression. Due to implementation constraints, this method can only be called as the last expression in a chain. Only one name operation per expression will work.

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() -> ExprT

Make the root column name uppercase.

Returns:

Type Description
ExprT

A new expression.

Notes

This will undo any previous renaming operations on the expression. Due to implementation constraints, this method can only be called as the last expression in a chain. Only one name operation per expression will work.

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']