Subsetting by Row and Column Number in Pandas – .iloc[] Best Practices 2026
When you need to select data by position (row number and column number) rather than by labels, Pandas provides the powerful .iloc[] indexer. In 2026, understanding .iloc[] is essential for tasks like taking the first N rows, selecting specific column ranges, or creating training/test splits.
TL;DR — .iloc[] Syntax
df.iloc[row_index]– Select by row positiondf.iloc[:, col_index]– Select by column positiondf.iloc[start:end, start:end]– Slice both rows and columns- Negative numbers count from the end
1. Basic Row and Column Subsetting with .iloc[]
import pandas as pd
df = pd.read_csv("sales_data.csv")
# First 10 rows
first_10 = df.iloc[:10]
# Last 5 rows
last_5 = df.iloc[-5:]
# Row 0 to 19, columns 2 to 5 (exclusive end)
subset = df.iloc[0:20, 2:6]
print(subset.head())
2. Selecting Specific Rows and Columns by Position
# Select rows 5, 10, 15, 20
selected_rows = df.iloc[[5, 10, 15, 20]]
# Select first 5 columns for first 100 rows
first_100_first_5_cols = df.iloc[:100, :5]
# Select last 3 columns for last 50 rows
last_50_last_3 = df.iloc[-50:, -3:]
3. Real-World Examples
# Create training and test sets by position
train = df.iloc[:8000] # First 80% as training
test = df.iloc[8000:] # Remaining as test
# Get only numeric columns (assuming they are in positions 3 to 7)
numeric_features = df.iloc[:, 3:8]
# Get the most recent 100 records and only key columns
recent = df.iloc[-100:, [0, 3, 5, 7]] # order_date, amount, region, customer_id
4. Best Practices in 2026
- Use
.iloc[]when you need to select by position (row/column number) - Use
.loc[]when selecting by labels (column names or index values) - Prefer
df[["col1", "col2"]]for selecting specific named columns when possible - Be careful with negative indexing —
-1means the last row/column - After heavy slicing, consider resetting the index with
.reset_index(drop=True)
Conclusion
Subsetting by row and column number using .iloc[] is essential when you need positional access. In 2026, use .iloc[] for tasks like taking the first/last N rows, creating train/test splits, or selecting columns by position. For most day-to-day work, prefer label-based selection with .loc[] or direct column selection, but .iloc[] remains the go-to tool whenever position matters.
Next steps:
- Practice using
.iloc[]to select the first 100 rows and last 5 columns of one of your datasets