narwhals.Expr.name
keep()
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 narwhals as nw
>>> from narwhals.typing import IntoFrameT
>>> 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:
>>> def my_library_agnostic_function(df_native: IntoFrameT) -> IntoFrameT:
... df = nw.from_native(df_native)
... return df.select(
... nw.col("foo").alias("alias_for_foo").name.keep()
... ).to_native()
We can then pass either pandas or Polars to func
:
>>> my_library_agnostic_function(df_pd).columns
Index(['foo'], dtype='object')
>>> my_library_agnostic_function(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 |
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 narwhals as nw
>>> from narwhals.typing import IntoFrameT
>>> 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
>>> def my_library_agnostic_function(df_native: IntoFrameT) -> IntoFrameT:
... df = nw.from_native(df_native)
... return df.select(nw.col("foo", "BAR").name.map(renaming_func)).to_native()
We can then pass either pandas or Polars to func
:
>>> my_library_agnostic_function(df_pd).columns
Index(['oof', 'RAB'], dtype='object')
>>> my_library_agnostic_function(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 |
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 narwhals as nw
>>> from narwhals.typing import IntoFrameT
>>> 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:
>>> def add_colname_prefix(df_native: IntoFrameT, prefix: str) -> IntoFrameT:
... df = nw.from_native(df_native)
... return df.select(nw.col("foo", "BAR").name.prefix(prefix)).to_native()
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 |
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 narwhals as nw
>>> from narwhals.typing import IntoFrameT
>>> 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:
>>> def add_colname_suffix(df_native: IntoFrameT, suffix: str) -> IntoFrameT:
... df = nw.from_native(df_native)
... return df.select(nw.col("foo", "BAR").name.suffix(suffix)).to_native()
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.
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 narwhals as nw
>>> from narwhals.typing import IntoFrameT
>>> 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:
>>> def to_lower(df_native: IntoFrameT) -> IntoFrameT:
... df = nw.from_native(df_native)
... return df.select(nw.col("foo", "BAR").name.to_lowercase()).to_native()
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.
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 narwhals as nw
>>> from narwhals.typing import IntoFrameT
>>> 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:
>>> def to_upper(df_native: IntoFrameT) -> IntoFrameT:
... df = nw.from_native(df_native)
... return df.select(nw.col("foo", "BAR").name.to_uppercase()).to_native()
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']