Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
df = pd.DataFrame({"num_legs": [4, 4, 2],
                   "num_arms": [0, 0, 2]},
                  ["fox", "deer", "Kangaroo"])
df
Out[2]:
num_legs num_arms
fox 4 0
deer 4 0
Kangaroo 2 2
In [3]:
df = df.rename_axis("animal")
df
Out[3]:
num_legs num_arms
animal
fox 4 0
deer 4 0
Kangaroo 2 2
In [4]:
df = df.rename_axis("limbs", axis="columns")
df
Out[4]:
limbs num_legs num_arms
animal
fox 4 0
deer 4 0
Kangaroo 2 2

MultiIndex

In [5]:
df.index = pd.MultiIndex.from_product([['mammal'],
                                       ['fox', 'deer', 'Kangaroo']],
                                       names=['type', 'name'])
df
Out[5]:
limbs num_legs num_arms
type name
mammal fox 4 0
deer 4 0
Kangaroo 2 2
In [6]:
df.rename_axis(index={'type': 'class'})
Out[6]:
limbs num_legs num_arms
class name
mammal fox 4 0
deer 4 0
Kangaroo 2 2
In [7]:
df.rename_axis(columns=str.upper)
Out[7]:
LIMBS num_legs num_arms
type name
mammal fox 4 0
deer 4 0
Kangaroo 2 2