Table Of Contents

Search

Enter search terms or a module, class or function name.

pandas.DataFrame.rename_axis

DataFrame.rename_axis(mapper=None, index=None, columns=None, axis=None, copy=True, inplace=False)[source]

Alter the name of the index or name of Index object that is the columns.

Parameters:

mapper : scalar, list-like, optional

Value to set the axis name attribute.

index, columns : scalar, list-like, dict-like or function, optional

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.

Changed in version 0.24.0.

axis : int or string, default 0

copy : boolean, default True

Also copy underlying data.

inplace : boolean, default False

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

Returns:

renamed : Series, DataFrame, or None

The same type as the caller or None if inplace is True.

See also

pandas.Series.rename
Alter Series index labels or name
pandas.DataFrame.rename
Alter DataFrame index labels or name
pandas.Index.rename
Set new names on index

Notes

Prior to version 0.21.0, rename_axis could also be used to change the axis labels by passing a mapping or scalar. This behavior is deprecated and will be removed in a future version. Use rename instead.

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. In this case, the parameter copy is ignored.

The second calling convention will modify the names of the the corresponding index if mapper is a list or a scalar. However, if mapper is dict-like or a function, it will use the deprecated behavior of modifying the axis labels.

We highly recommend using keyword arguments to clarify your intent.

Examples

Series

>>> s = pd.Series([1, 2, 3])
>>> s.rename_axis("foo")
foo
0    1
1    2
2    3
dtype: int64

DataFrame

>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
>>> df.rename_axis("foo")
     A  B
foo
0    1  4
1    2  5
2    3  6
>>> df.rename_axis("bar", axis="columns")
bar  A  B
0    1  4
1    2  5
2    3  6
>>> mi = pd.MultiIndex.from_product([['a', 'b', 'c'], [1, 2]],
...                                 names=['let','num'])
>>> df = pd.DataFrame({'x': [i for i in range(len(mi))],
...                    'y' : [i*10 for i in range(len(mi))]},
...                    index=mi)
>>> df.rename_axis(index={'num' : 'n'})
       x   y
let n
a   1  0   0
    2  1  10
b   1  2  20
    2  3  30
c   1  4  40
    2  5  50
>>> cdf = df.rename_axis(columns='col')
>>> cdf
col      x   y
let num
a   1    0   0
    2    1  10
b   1    2  20
    2    3  30
c   1    4  40
    2    5  50
>>> cdf.rename_axis(columns=str.upper)
COL      x   y
let num
a   1    0   0
    2    1  10
b   1    2  20
    2    3  30
c   1    4  40
    2    5  50
Scroll To Top