๐Ÿ€Zerve chosen as NCAA's Agentic Data Platform for 2026 Hackathonยท๐ŸงฎMeet the Zerve Team at Data Decoded Londonยท๐Ÿ“ˆWe're hiring โ€” awesome new roles just gone live!
Back
Pandas

IndexError: Single Positional Indexer is Out-of-Bounds โ€” How to Fix It

Answer

This error means you're using .iloc[] to access a row or column by position that doesn't exist. Fix it by checking the shape of your dataframe first โ€” you're trying to access an index beyond the actual number of rows or columns.

Why This Happens

.iloc[] uses integer positions starting from 0. If your dataframe has 10 rows (indexes 0-9) and you try to access df.iloc[10], it fails. This commonly happens after filtering (fewer rows than expected), off-by-one errors, or assuming a dataframe is larger than it is.

Solution

The rule: always check df.shape or use negative indexing (-1 for last) instead of hardcoding positions.

import pandas as pd

df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})

# โŒ Problematic: accessing row that doesn't exist
df.iloc[5]
# IndexError: single positional indexer is out-of-bounds

# โœ… Fixed: check shape first
print(df.shape)  # (3, 2) โ€” only 3 rows, indexes 0-2
df.iloc[2]  # last valid row

# โœ… Fixed: use -1 for last row instead of guessing
df.iloc[-1]  # always gets last row regardless of length

# โœ… Safe access: check length before indexing
idx = 5
if idx < len(df):
    row = df.iloc[idx]

Better Workflow

Zerve displays dataframe shape inline after each cell, so you can see exactly how many rows you're working with before attempting positional access.

Better workflow

Related Topics

Decision-grade data work

Explore, analyze and deploy your first project in minutes