Select Pandas rows based on list index
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Selecting rows by a list of indexes is a basic Pandas task, but it is also a common source of mistakes because Pandas supports both positional indexing and label-based indexing. The right method depends on whether your list contains row positions or actual index labels.
Use .iloc for Integer Positions
If your list means "give me the first, third, and fifth rows," use .iloc. It treats the values as zero-based positions regardless of the DataFrame's index labels.
Output:
.iloc also preserves the order of the list you pass in. If the list is [4, 0], the result will return Eve first and Alice second.
Use .loc for Index Labels
If your DataFrame has a custom index, row selection changes. In that case, use .loc when the list contains labels rather than positions.
Output:
This is an important distinction: .loc does not mean "rows with these row numbers." It means "rows whose index labels match these values."
Selecting Safely When Indexes May Be Missing
Both .iloc and .loc raise an error when the requested rows do not exist. That is often desirable because it exposes bad assumptions early. If you want missing labels to produce NaN rows instead, use reindex.
Output:
That behavior is useful when you are aligning data from another source and want to preserve the requested order even if some rows are missing.
Filtering With a List of Conditions
Sometimes developers say "list index" when they actually have a boolean list, not a list of row numbers. That is a different operation. In that case, pass the boolean mask directly.
A boolean list must be the same length as the DataFrame. If it is not, Pandas raises an error.
Common Pitfalls
The most common mistake is confusing .loc and .iloc. If your DataFrame index happens to be integers, the bug can be subtle because both methods may run while meaning different things.
Another pitfall is assuming the DataFrame index is always the default 0, 1, 2 sequence. After filtering, sorting, or loading data from a file, the index may no longer match row positions. When you need position-based access after those operations, .iloc is usually the safer choice.
Out-of-range values also matter. With .iloc, requesting position 10 in a five-row DataFrame raises IndexError. With .loc, missing labels raise KeyError unless you use reindex.
Finally, watch out for duplicates. If your list contains repeated positions or labels, Pandas will repeat those rows in the result. That is sometimes exactly what you want, but it surprises people who expect automatic deduplication.
Summary
- Use
.ilocwhen your list contains row positions. - Use
.locwhen your list contains index labels. - Use
reindexwhen you want missing labels to stay in the result instead of raising an error. - Boolean masks are a separate selection mechanism and must match the DataFrame length.
- Always confirm whether your DataFrame index still matches row positions before selecting rows by a list.
Related reading
- Select row with most recent date per user
- Select rows in pandas MultiIndex DataFrame
- Selecting a row of pandas series/dataframe by integer index
- Selecting multiple columns in a Pandas dataframe
- Select records from NOW -1 Day
- Select rows from a table that are not in another
- Selecting with complex criteria from pandas.DataFrame
- Selecting/excluding sets of columns in pandas

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.