How to Use iloc in pandas for Data Selection
Use
iloc in pandas to select rows and columns by their integer position. It accepts row and column indices in the format df.iloc[row_index, column_index] to extract data by position, not label.Syntax
The basic syntax of iloc is df.iloc[row_indexer, column_indexer]. You can use integers, slices, or lists of integers to specify which rows and columns to select.
- row_indexer: integer, list, or slice for rows by position
- column_indexer: integer, list, or slice for columns by position
Both row and column indexers are zero-based and exclusive for slices on the stop index.
python
df.iloc[0, 1] # Selects first row, second column df.iloc[1:3, 0:2] # Selects rows 2-3 and columns 1-2 df.iloc[[0, 2], [1, 3]] # Selects specific rows and columns by position
Example
This example shows how to select specific rows and columns using iloc on a sample DataFrame.
python
import pandas as pd # Create sample DataFrame data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [25, 30, 35, 40], 'City': ['NY', 'LA', 'Chicago', 'Houston']} df = pd.DataFrame(data) # Select first row, second column (Age) age_first = df.iloc[0, 1] # Select rows 1 to 2 and columns 0 to 1 (Name and Age for Bob and Charlie) subset = df.iloc[1:3, 0:2] (age_first, subset)
Output
(25, Name Age
1 Bob 30
2 Charlie 35)
Common Pitfalls
Common mistakes when using iloc include:
- Using labels instead of integer positions (use
locfor labels). - Forgetting that slicing with
ilocexcludes the stop index. - Mixing row and column indexers incorrectly (always use
df.iloc[row_indexer, column_indexer]).
python
import pandas as pd data = {'A': [10, 20, 30], 'B': [40, 50, 60]} df = pd.DataFrame(data) # Wrong: Using label with iloc (raises error) # df.iloc['A', 0] # Right: Use integer position value = df.iloc[0, 0] # Correct: Slicing excludes stop index subset_correct = df.iloc[0:2, 0:2] # selects rows 0 and 1, not 2 (value, subset_correct)
Output
(10, A B
0 10 40
1 20 50)
Quick Reference
| Usage | Description | Example |
|---|---|---|
| Single element | Select one cell by row and column position | df.iloc[0, 1] |
| Row slice | Select multiple rows, all columns | df.iloc[1:4, :] |
| Column slice | Select all rows, multiple columns | df.iloc[:, 0:2] |
| List of positions | Select specific rows and columns | df.iloc[[0,2], [1,3]] |
| Negative indices | Select from the end | df.iloc[-1, -1] |
Key Takeaways
Use iloc to select data by integer position, not by label.
Row and column indices in iloc start at zero and slices exclude the stop index.
Always use df.iloc[row_indexer, column_indexer] with integers, slices, or lists.
For label-based selection, use df.loc instead of iloc.
Be careful not to mix labels and positions to avoid errors.