pyspark.pandas.DataFrame.rename_axis

DataFrame.rename_axis(mapper: Union[Any, Sequence[Any], Dict[Union[Any, Tuple[Any, …]], Any], Callable[[Union[Any, Tuple[Any, …]]], Any]] = None, index: Union[Any, Sequence[Any], Dict[Union[Any, Tuple[Any, …]], Any], Callable[[Union[Any, Tuple[Any, …]]], Any]] = None, columns: Union[Any, Sequence[Any], Dict[Union[Any, Tuple[Any, …]], Any], Callable[[Union[Any, Tuple[Any, …]]], Any]] = None, axis: Union[int, str, None] = 0, inplace: Optional[bool] = False) → Optional[pyspark.pandas.frame.DataFrame][source]

Set the name of the axis for the index or columns.

Parameters
mapperscalar, list-like, optional

A scalar, list-like, dict-like or functions transformations to apply to the axis name attribute.

index, columnsscalar, list-like, dict-like or function, optional

A scalar, list-like, dict-like or functions transformations to apply to that axis’ values.

Use either mapper and axis to specify the axis to target with mapper, or index and/or columns.

axis{0 or ‘index’, 1 or ‘columns’}, default 0

The axis to rename.

inplacebool, default False

Modifies the object directly, instead of creating a new DataFrame.

Returns
DataFrame, or None if inplace is True.

See also

Series.rename

Alter Series index labels or name.

DataFrame.rename

Alter DataFrame index labels or name.

Index.rename

Set new names on index.

Notes

DataFrame.rename_axis supports two calling conventions

  • (index=index_mapper, columns=columns_mapper, ...)

  • (mapper, axis={'index', 'columns'}, ...)

The first calling convention will only modify the names of the index and/or the names of the Index object that is the columns.

The second calling convention will modify the names of the corresponding index specified by axis.

We highly recommend using keyword arguments to clarify your intent.

Examples

>>> df = ps.DataFrame({"num_legs": [4, 4, 2],
...                    "num_arms": [0, 0, 2]},
...                   index=["dog", "cat", "monkey"],
...                   columns=["num_legs", "num_arms"])
>>> df
        num_legs  num_arms
dog            4         0
cat            4         0
monkey         2         2
>>> df = df.rename_axis("animal").sort_index()
>>> df  
        num_legs  num_arms
animal
cat            4         0
dog            4         0
monkey         2         2
>>> df = df.rename_axis("limbs", axis="columns").sort_index()
>>> df 
limbs   num_legs  num_arms
animal
cat            4         0
dog            4         0
monkey         2         2

MultiIndex

>>> index = pd.MultiIndex.from_product([['mammal'],
...                                     ['dog', 'cat', 'monkey']],
...                                    names=['type', 'name'])
>>> df = ps.DataFrame({"num_legs": [4, 4, 2],
...                    "num_arms": [0, 0, 2]},
...                   index=index,
...                   columns=["num_legs", "num_arms"])
>>> df  
               num_legs  num_arms
type   name
mammal dog            4         0
       cat            4         0
       monkey         2         2
>>> df.rename_axis(index={'type': 'class'}).sort_index()  
               num_legs  num_arms
class  name
mammal cat            4         0
       dog            4         0
       monkey         2         2
>>> df.rename_axis(index=str.upper).sort_index()  
               num_legs  num_arms
TYPE   NAME
mammal cat            4         0
       dog            4         0
       monkey         2         2