Examples
import numpy as np
import pandas as pd
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 }]
df = pd.DataFrame(mydict)
df
Indexing just the rows
With a scalar integer.
type(df.iloc[0])
df.iloc[0]
With a list of integers.
df.iloc[[0]]
type(df.iloc[[0]])
df.iloc[[0, 2]]
With a slice object.
df.iloc[:3]
With a boolean mask the same length as the index.
df.iloc[[True, False, True]]
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.
df.iloc[lambda x: x.index % 2 == 0]
Indexing both axes
You can mix the indexer types for the index and columns. Use : to select the entire axis.
With scalar integers.
df.iloc[0, 2]
With lists of integers.
df.iloc[[0, 2], [1, 3]]
With slice objects.
df.iloc[1:3, 0:3]
With a boolean array whose length matches the columns.
df.iloc[:, [True, False, True, False]]
With a callable function that expects the Series or DataFrame.
df.iloc[:, lambda df: [0, 2]]