Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
mydict = [{'p': 2, 'q': 3, 'r': 4, 's': 5},
           {'p': 20, 'q': 30, 'r': 40, 's': 50},
           {'p': 200, 'q': 300, 'r': 400, 's': 500 }]
In [3]:
df = pd.DataFrame(mydict)
df
Out[3]:
p q r s
0 2 3 4 5
1 20 30 40 50
2 200 300 400 500

Indexing just the rows
With a scalar integer.

In [4]:
type(df.iloc[0])
Out[4]:
pandas.core.series.Series
In [5]:
df.iloc[0]
Out[5]:
p    2
q    3
r    4
s    5
Name: 0, dtype: int64

With a list of integers.

In [6]:
df.iloc[[0]]
Out[6]:
p q r s
0 2 3 4 5
In [7]:
type(df.iloc[[0]])
Out[7]:
pandas.core.frame.DataFrame
In [8]:
df.iloc[[0, 2]]
Out[8]:
p q r s
0 2 3 4 5
2 200 300 400 500

With a slice object.

In [9]:
df.iloc[:3]
Out[9]:
p q r s
0 2 3 4 5
1 20 30 40 50
2 200 300 400 500

With a boolean mask the same length as the index.

In [10]:
df.iloc[[True, False, True]]
Out[10]:
p q r s
0 2 3 4 5
2 200 300 400 500

With a callable, useful in method chains. The x passed to the lambda is the DataFrame being sliced.
This selects the rows whose index label even.

In [11]:
df.iloc[lambda x: x.index % 2 == 0]
Out[11]:
p q r s
0 2 3 4 5
2 200 300 400 500

Indexing both axes
You can mix the indexer types for the index and columns. Use : to select the entire axis.
With scalar integers.

In [12]:
df.iloc[0, 2]
Out[12]:
4

With lists of integers.

In [13]:
df.iloc[[0, 2], [1, 3]]
Out[13]:
q s
0 3 5
2 300 500

With slice objects.

In [14]:
df.iloc[1:3, 0:3]
Out[14]:
p q r
1 20 30 40
2 200 300 400

With a boolean array whose length matches the columns.

In [15]:
df.iloc[:, [True, False, True, False]]
Out[15]:
p r
0 2 4
1 20 40
2 200 400

With a callable function that expects the Series or DataFrame.

In [16]:
df.iloc[:, lambda df: [0, 2]]
Out[16]:
p r
0 2 4
1 20 40
2 200 400