Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
df = pd.DataFrame({"P": [2, 3, 4], "Q": [5, 6, 7]})

Change the row labels.

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

Change the column labels:

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

Let update the labels inplace:

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