Examples
Series

In [1]:
import numpy as np
import pandas as pd
In [2]:
s = pd.Series([2, 3, 4])
s
Out[2]:
0    2
1    3
2    4
dtype: int64

In [3]:
s.set_axis(['p', 'q', 'r'], axis=0, inplace=False)
Out[3]:
p    2
q    3
r    4
dtype: int64

The original object is not modified.

In [4]:
s
Out[4]:
0    2
1    3
2    4
dtype: int64

DataFrame

In [5]:
df = pd.DataFrame({"X": [2, 3, 4], "Y": [5, 6, 7]})

Change the row labels.

In [6]:
df.set_axis(['p', 'q', 'r'], axis='index', inplace=False)
Out[6]:
X Y
p 2 5
q 3 6
r 4 7

Change the column labels.

In [7]:
df.set_axis(['I', 'II'], axis='columns', inplace=False)
Out[7]:
I II
0 2 5
1 3 6
2 4 7

Now, update the labels inplace.

In [8]:
df.set_axis(['i', 'ii'], axis='columns', inplace=True)
df
Out[8]:
i ii
0 2 5
1 3 6
2 4 7