How to get the last N rows of a pandas DataFrame?
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
Getting the last N rows of a pandas DataFrame is a routine operation when you inspect recent records, debug a transformation, or preview the end of a dataset. Pandas makes this easy with .tail(), and it also offers indexing-based alternatives when you need more control.
The Standard Solution: tail
The most direct method is DataFrame.tail(n).
Output:
tail(2) returns the last two rows in their original order. If you omit the argument, pandas returns the last five rows by default.
Using iloc Slicing
You can achieve the same result with integer-location indexing:
This is useful when you are already working with iloc and want slicing syntax for consistency. It behaves similarly to Python list slicing from the end.
What Happens When N Is Large
Both .tail(n) and df.iloc[-n:] are forgiving when n is larger than the DataFrame length. Instead of raising an error, pandas returns the whole DataFrame.
That behavior is convenient in reporting code because you do not need a separate bounds check for many cases.
When "Last" Does Not Mean "Latest"
A common conceptual bug is assuming the last rows are the most recent rows. Pandas preserves row order; it does not automatically sort by time. If your data is not already ordered by a timestamp, sort first and then take the tail.
Without the sort, you would only get the last rows in current storage order, which may not match the latest timestamps.
Resetting the Index if Needed
The returned slice keeps the original index values. That is usually correct, but sometimes you want a clean zero-based index for display or downstream processing.
Use this only when reindexing is desirable. Preserving the original index can be important for joins or debugging.
Last Rows Within Each Group
Sometimes you do not want the last N rows of the whole DataFrame. You want the last row or last few rows inside each group, such as the latest record per customer. In that case, combine grouping with .tail().
This is a different problem from taking the final rows of the full DataFrame, but it uses the same tail concept and is worth keeping in mind.
Choosing Between tail and iloc
For readability, .tail(n) is usually the best choice because it states the intent directly. iloc is more flexible when you are composing larger index-based selections.
In other words:
- use
.tail(n)for clarity - use
ilocwhen you are already in an index-slicing workflow
Both are efficient enough for ordinary DataFrame inspection and transformation tasks.
Common Pitfalls
Assuming the last rows are the newest rows without sorting by a timestamp can give misleading results.
Forgetting that the original index is preserved can be confusing when the slice prints row labels such as 98 and 99.
Passing a negative n changes the semantics in ways many people do not intend, so keep the argument positive for "last N rows" logic.
Using label-based .loc instead of .iloc for negative slicing will not behave the same way.
Overcomplicating the task with manual loops is unnecessary because pandas already has a direct built-in method.
Summary
- Use
df.tail(n)to get the lastNrows of a DataFrame. - '
df.iloc[-n:]is the equivalent indexing-based form.' - If
nexceeds the DataFrame length, pandas returns all rows. - Sort by a time column first if "last" should mean "latest".
- Reset the index only when you want a fresh sequential display.
Related reading
- How to get the Stackdriver charts into a csv file?
- How to get the value of a tensor? Python
- How to get the weight vector in Logistic Regression?
- How to get/set a pandas index column title or name?
- How to get the line count of a large file cheaply in Python
- How to get the line count of a large file cheaply in Python
- How to graph grid scores from GridSearchCV?
- How to group dataframe rows into list in pandas groupby
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free 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.