Skip to content

narwhals.Expr.name

keep()

Keep the original root name of the 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 narwhals as nw
>>> import pandas as pd
>>> import polars as pl
>>> data = {"foo": [1, 2], "BAR": [4, 5]}
>>> df_pd = pd.DataFrame(data)
>>> df_pl = pl.DataFrame(data)

We define a dataframe-agnostic function:

>>> @nw.narwhalify
... def func(df):
...     return df.select(nw.col("foo").alias("alias_for_foo").name.keep())

We can then pass either pandas or Polars to func:

>>> func(df_pd).columns
Index(['foo'], dtype='object')
>>> func(df_pl).columns
['foo']

map(function)

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

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 narwhals as nw
>>> import pandas as pd
>>> import polars as pl
>>> data = {"foo": [1, 2], "BAR": [4, 5]}
>>> df_pd = pd.DataFrame(data)
>>> df_pl = pl.DataFrame(data)

We define a dataframe-agnostic function:

>>> renaming_func = lambda s: s[::-1]  # reverse column name
>>> @nw.narwhalify
... def func(df):
...     return df.select(nw.col("foo", "BAR").name.map(renaming_func))

We can then pass either pandas or Polars to func:

>>> func(df_pd).columns
Index(['oof', 'RAB'], dtype='object')
>>> func(df_pl).columns
['oof', 'RAB']

prefix(prefix)

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

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 narwhals as nw
>>> import pandas as pd
>>> import polars as pl
>>> data = {"foo": [1, 2], "BAR": [4, 5]}
>>> df_pd = pd.DataFrame(data)
>>> df_pl = pl.DataFrame(data)

We define a dataframe-agnostic function:

>>> @nw.narwhalify
... def add_colname_prefix(df, prefix):
...     return df.select(nw.col("foo", "BAR").name.prefix(prefix))

We can then pass either pandas or Polars to func:

>>> add_colname_prefix(df_pd, "with_prefix_").columns
Index(['with_prefix_foo', 'with_prefix_BAR'], dtype='object')
>>> add_colname_prefix(df_pl, "with_prefix_").columns
['with_prefix_foo', 'with_prefix_BAR']

suffix(suffix)

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

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 narwhals as nw
>>> import pandas as pd
>>> import polars as pl
>>> data = {"foo": [1, 2], "BAR": [4, 5]}
>>> df_pd = pd.DataFrame(data)
>>> df_pl = pl.DataFrame(data)

We define a dataframe-agnostic function:

>>> @nw.narwhalify
... def add_colname_suffix(df, suffix):
...     return df.select(nw.col("foo", "BAR").name.suffix(suffix))

We can then pass either pandas or Polars to func:

>>> add_colname_suffix(df_pd, "_with_suffix").columns
Index(['foo_with_suffix', 'BAR_with_suffix'], dtype='object')
>>> add_colname_suffix(df_pl, "_with_suffix").columns
['foo_with_suffix', 'BAR_with_suffix']

to_lowercase()

Make the root column name lowercase.

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 narwhals as nw
>>> import pandas as pd
>>> import polars as pl
>>> data = {"foo": [1, 2], "BAR": [4, 5]}
>>> df_pd = pd.DataFrame(data)
>>> df_pl = pl.DataFrame(data)

We define a dataframe-agnostic function:

>>> @nw.narwhalify
... def to_lower(df):
...     return df.select(nw.col("foo", "BAR").name.to_lowercase())

We can then pass either pandas or Polars to func:

>>> to_lower(df_pd).columns
Index(['foo', 'bar'], dtype='object')
>>> to_lower(df_pl).columns
['foo', 'bar']

to_uppercase()

Make the root column name uppercase.

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 narwhals as nw
>>> import pandas as pd
>>> import polars as pl
>>> data = {"foo": [1, 2], "BAR": [4, 5]}
>>> df_pd = pd.DataFrame(data)
>>> df_pl = pl.DataFrame(data)

We define a dataframe-agnostic function:

>>> @nw.narwhalify
... def to_upper(df):
...     return df.select(nw.col("foo", "BAR").name.to_uppercase())

We can then pass either pandas or Polars to func:

>>> to_upper(df_pd).columns
Index(['FOO', 'BAR'], dtype='object')
>>> to_upper(df_pl).columns
['FOO', 'BAR']