Examples

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

Indexing just the rows
With a scalar integer.

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

With a list of integers.

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

With a slice object:

In [8]:
df.iloc[:3]
Out[8]:
p q r s
0 2 3 4 5
1 200 300 400 500
2 2000 3000 4000 5000

With a boolean mask the same length as the index:

In [9]:
df.iloc[[True, False, True]]
Out[9]:
p q r s
0 2 3 4 5
2 2000 3000 4000 5000

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 [10]:
df.iloc[lambda x: x.index % 2 == 0]
Out[10]:
p q r s
0 2 3 4 5
2 2000 3000 4000 5000

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

In [11]:
df.iloc[0, 1]
Out[11]:
3

With lists of integers:

In [12]:
df.iloc[[0, 2], [2, 3]]
Out[12]:
r s
0 4 5
2 4000 5000

With slice objects:

In [13]:
df.iloc[1:3, 0:3]
Out[13]:
p q r
1 200 300 400
2 2000 3000 4000

With a boolean array whose length matches the columns:

In [14]:
df.iloc[:, [True, False, True, False]]
Out[14]:
p r
0 2 4
1 200 400
2 2000 4000

With a callable function that expects the Series or DataFrame.

In [15]:
df.iloc[:, lambda df: [0, 2]]
Out[15]:
p r
0 2 4
1 200 400
2 2000 4000