Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
d = {'num_legs': [4, 4, 2, 2],
     'num_wings': [0, 0, 2, 2],
     'class': ['mammal', 'mammal', 'bird', 'bird'],
     'animal': ['tiger', 'fox', 'penguin', 'sparrow'],
     'locomotion': ['walks', 'walks', 'walks', 'flies']}
df = pd.DataFrame(data=d)
In [3]:
df = df.set_index(['class', 'animal', 'locomotion'])
df
Out[3]:
num_legs num_wings
class animal locomotion
mammal tiger walks 4 0
fox walks 4 0
bird penguin walks 2 2
sparrow flies 2 2

Get values at specified index

In [4]:
df.xs('mammal')
Out[4]:
num_legs num_wings
animal locomotion
tiger walks 4 0
fox walks 4 0

Get values at several indexes:

In [9]:
df.xs(('mammal', 'fox'))
Out[9]:
num_legs num_wings
locomotion
walks 4 0

Get values at specified index and level:

In [6]:
df.xs('tiger', level=1)
Out[6]:
num_legs num_wings
class locomotion
mammal walks 4 0

Get values at several indexes and levels

In [7]:
df.xs(('bird', 'walks'),
      level=[0, 'locomotion'])
Out[7]:
num_legs num_wings
animal
penguin 2 2

Get values at specified column and axis:

In [8]:
df.xs('num_wings', axis=1)
Out[8]:
class   animal   locomotion
mammal  tiger    walks         0
        fox      walks         0
bird    penguin  walks         2
        sparrow  flies         2
Name: num_wings, dtype: int64