Head and Tail in Pandas
Learn how to preview the first and last rows of a DataFrame using Pandas.
1. Introduction to head() and tail()
When working with a dataset, you often need to examine a few records before performing calculations or analysis. Printing every row is not practical for large datasets.
Pandas provides two useful methods for this purpose:
head() and tail().
head()
Displays the first rows of a DataFrame or Series.
tail()
Displays the last rows of a DataFrame or Series.
2. Create a Sample DataFrame
We will use student records to understand both methods.
import pandas as pd
data = {
"Name": [
"Samadhan",
"Pradnya",
"Samiksha",
"Aarav",
"Neha",
"Rahul",
"Priya",
"Amit"
],
"Marks": [85, 92, 88, 76, 95, 81, 90, 78],
"City": [
"Pune",
"Mumbai",
"Nashik",
"Pune",
"Mumbai",
"Nashik",
"Pune",
"Mumbai"
]
}
df = pd.DataFrame(data)
print(df)
Output
Name Marks City 0 Samadhan 85 Pune 1 Pradnya 92 Mumbai 2 Samiksha 88 Nashik 3 Aarav 76 Pune 4 Neha 95 Mumbai 5 Rahul 81 Nashik 6 Priya 90 Pune 7 Amit 78 Mumbai
This DataFrame contains eight records. The default index runs from 0 to 7.
3. Using head()
The head() method returns the first five rows
when called without an argument.
print(df.head())
Output
Name Marks City 0 Samadhan 85 Pune 1 Pradnya 92 Mumbai 2 Samiksha 88 Nashik 3 Aarav 76 Pune 4 Neha 95 Mumbai
Explanation
- Only the first five rows are displayed.
- The original row indexes are preserved.
- The original DataFrame is not modified.
- The remaining rows are not included in this preview.
Display the first three rows
print(df.head(3))
Output
Name Marks City 0 Samadhan 85 Pune 1 Pradnya 92 Mumbai 2 Samiksha 88 Nashik
Here, the argument 3 tells Pandas to return only
the first three rows.
4. Image 1: How head() Selects Rows
The highlighted rows in this illustration represent the rows
returned by df.head(3).
Image 1: head(3) returns rows at the beginning of the DataFrame.
5. Using tail()
The tail() method returns the last five rows
by default. It is useful for checking the end of a dataset
or reviewing recently appended records.
print(df.tail())
Output
Name Marks City 3 Aarav 76 Pune 4 Neha 95 Mumbai 5 Rahul 81 Nashik 6 Priya 90 Pune 7 Amit 78 Mumbai
Display the last three rows
print(df.tail(3))
Output
Name Marks City 5 Rahul 81 Nashik 6 Priya 90 Pune 7 Amit 78 Mumbai
Explanation
tail(3)returns the last three rows.- The records appear in their original order.
- The original index labels remain unchanged.
- The original DataFrame is not modified.
6. Image 2: How tail() Selects Rows
The highlighted rows below represent the last three rows
returned by df.tail(3).
Image 2: tail() returns records from the end of the DataFrame.
df.tail(3) returns index
labels 5, 6, and 7: Rahul, Priya, and Amit.
7. Difference Between head() and tail()
| Feature | head() | tail() |
|---|---|---|
| Purpose | Displays rows from the beginning. | Displays rows from the end. |
| Default row count | 5 | 5 |
| Custom count | df.head(3) |
df.tail(3) |
| Common use | Check initial records. | Check final records. |
| Changes original DataFrame? | No | No |
# First two rows print(df.head(2)) # Last two rows print(df.tail(2))
8. What Happens When You Change the Number?
The number passed to head() or tail()
controls how many rows are requested.
print(df.head(1)) print(df.tail(1)) print(df.head(10)) print(df.tail(10))
Since the DataFrame contains eight rows, requesting ten rows returns all available rows. These methods do not create extra records to reach the requested number.
df.head(-2) returns all but the last
two rows, while df.tail(-2) returns all but the
first two rows.
9. Using head() and tail() with a Series
These methods are also available on a Pandas Series. A Series represents a single labeled sequence of values.
marks = df["Marks"] print(marks.head(3)) print(marks.tail(3))
Output of marks.head(3)
0 85 1 92 2 88 Name: Marks, dtype: int64
Output of marks.tail(3)
5 81 6 90 7 78 Name: Marks, dtype: int64
10. Practical Example: Inspect a CSV File
When loading a CSV file, it is useful to preview the records immediately to check whether the file was read as expected.
import pandas as pd
df = pd.read_csv("students.csv")
print("First records:")
print(df.head())
print("Last records:")
print(df.tail())
This code assumes that students.csv exists in the
current working directory. If it is stored elsewhere, provide
the correct file path.
df.shape and df.info().
11. Common Mistakes
Mistake 1: Forgetting the parentheses
# Incorrect: refers to the method itself print(df.head) # Correct: calls the method print(df.head())
Mistake 2: Assuming the default is all rows
df.head() and df.tail() normally
return up to five rows, not the complete dataset.
Mistake 3: Assuming the original data is modified
These methods return a preview. They do not delete the remaining rows from the original DataFrame.
Mistake 4: Confusing row count with index labels
The index labels shown in the output identify the rows. They do not mean that the returned preview starts at index 0.
12. Hands-on Practice Exercise
Samiksha is analyzing a dataset containing student records. Use the following DataFrame to complete the exercises.
import pandas as pd
df = pd.DataFrame({
"Name": ["Samadhan", "Pradnya", "Samiksha", "Aarav", "Neha", "Rahul"],
"Marks": [85, 92, 88, 76, 95, 81],
"City": ["Pune", "Mumbai", "Nashik", "Pune", "Mumbai", "Nashik"]
})
Tasks
- Display the first five rows.
- Display the first two rows.
- Display the last three rows.
- Display only the first row.
- Display the first ten requested rows and observe the result.
- Extract the Marks column and display its last two values.
Suggested solution
print(df.head()) print(df.head(2)) print(df.tail(3)) print(df.head(1)) print(df.head(10)) marks = df["Marks"] print(marks.tail(2))
13. Revision Questions
- What is the purpose of
head()? - What is the purpose of
tail()? - How many rows do both methods return by default?
- How do you display the first three rows?
- How do you display the last four rows?
- Do these methods modify the original DataFrame?
- Can you use
head()andtail()on a Series? - What happens when you request more rows than the dataset contains?
Summary
Pandas provides head() and tail() to
preview records at the beginning and end of a DataFrame or Series.
Both methods return five rows by default, and you can pass a
number to control the preview size.
Use head() to inspect initial records and
tail() to inspect final records. These methods
return a preview without modifying the original DataFrame.
After mastering row previews, continue to DataFrame structure, data types, and descriptive statistics.