head() and tail() help you quickly see the first or last few rows of your data. This is useful to understand what your data looks like without looking at everything.
You want to check the first few entries in a sales dataset to verify data loading.
You want to see the last few rows of a log file to check recent events.
You want to quickly preview a large dataset without printing all rows.
You want to confirm the structure and columns of your data by looking at sample rows.
Syntax
Data Analysis Python
DataFrame.head(n=5)
DataFrame.tail(n=5)
Both methods return a new DataFrame with n rows.
If n is not given, it defaults to 5 rows.
Examples
Shows the first 5 rows of the DataFrame df.
Data Analysis Python
df.head()
Shows the first 3 rows of df.
Data Analysis Python
df.head(3)
Shows the last 5 rows of df.
Data Analysis Python
df.tail()
Shows the last 2 rows of df.
Data Analysis Python
df.tail(2)
Sample Program
This code creates a small table of people with their ages and cities. It then shows the first 3 rows and the last 2 rows using head() and tail().
Data Analysis Python
import pandas as pd
# Create a simple DataFrame
data = {'Name': ['Anna', 'Bob', 'Charlie', 'David', 'Eva', 'Frank'],
'Age': [23, 35, 45, 32, 28, 40],
'City': ['NY', 'LA', 'Chicago', 'Houston', 'Phoenix', 'Seattle']}
df = pd.DataFrame(data)
print('First 3 rows using head(3):')
print(df.head(3))
print('\nLast 2 rows using tail(2):')
print(df.tail(2))
OutputSuccess
Important Notes
If your DataFrame has fewer rows than n, head() or tail() will just return all rows.
These methods are very useful for quick data checks before deeper analysis.
Summary
head() shows the first few rows of your data.
tail() shows the last few rows of your data.
Both help you quickly understand your dataset's content and structure.
Practice
(1/5)
1. What does the head() function do when used on a DataFrame?
easy
A. Shows the last few rows of the DataFrame
B. Deletes the first few rows of the DataFrame
C. Shows the total number of rows in the DataFrame
D. Shows the first few rows of the DataFrame
Solution
Step 1: Understand the purpose of head()
The head() function is designed to display the first few rows of a DataFrame to give a quick look at the data.
Step 2: Compare with other options
tail() shows the last few rows, counting rows is done by len(), and deleting rows is not the function of head().
Final Answer:
Shows the first few rows of the DataFrame -> Option D
Quick Check:
head() = first rows [OK]
Hint: Remember: head = start, tail = end [OK]
Common Mistakes:
Confusing head() with tail()
Thinking head() counts rows
Assuming head() deletes data
2. Which of the following is the correct syntax to show the last 3 rows of a DataFrame named df?
easy
A. df.tail(3)
B. df.head(3)
C. df.last(3)
D. df.end(3)
Solution
Step 1: Identify the function for last rows
The tail() function returns the last rows of a DataFrame, and it accepts a number to specify how many rows.
Step 2: Check syntax correctness
df.tail(3) is correct; head() shows first rows, and last() or end() are not valid pandas functions.
head(2) returns the first 2 rows of the DataFrame.
Step 2: Identify the first 2 rows in the DataFrame
The first two rows have Names 'Anna' and 'Bob' with Ages 23 and 35 respectively.
Final Answer:
Rows with Name Anna and Bob -> Option C
Quick Check:
head(2) = first 2 rows [OK]
Hint: head(n) shows first n rows [OK]
Common Mistakes:
Thinking head() shows last rows
Assuming head() cannot take arguments
Mixing row order
4. What is wrong with this code snippet?
import pandas as pd
data = {'A': [1, 2, 3, 4, 5]}
df = pd.DataFrame(data)
print(df.tail[3])
medium
A. DataFrame creation is incorrect
B. Using square brackets instead of parentheses with tail
C. tail() cannot be used with numbers
D. Missing import statement
Solution
Step 1: Check the method call syntax
The code uses df.tail[3] which uses square brackets. Methods require parentheses.
Step 2: Correct the syntax
It should be df.tail(3) to call the method with argument 3.
Final Answer:
Using square brackets instead of parentheses with tail -> Option B
Quick Check:
Method calls need parentheses [OK]
Hint: Use parentheses () to call functions [OK]
Common Mistakes:
Using [] instead of () for method calls
Assuming tail() can't take arguments
Ignoring Python syntax errors
5. You have a DataFrame df with 1000 rows. You want to quickly check the first 5 and last 5 rows together. Which code snippet correctly shows this?
hard
A. pd.concat([df.head(), df.tail()])
B. df.head() + df.tail()
C. df.head() & df.tail()
D. df.head().append(df.tail())
Solution
Step 1: Understand how to combine DataFrames
To combine two DataFrames vertically, use pd.concat() or append() method.
Step 2: Check each option
df.head() + df.tail() and df.head() & df.tail() are invalid operations for DataFrames. df.head().append(df.tail()) works but is deprecated in recent pandas versions. pd.concat([df.head(), df.tail()]) is the recommended way.
Final Answer:
pd.concat([df.head(), df.tail()]) -> Option A
Quick Check:
Use pd.concat() to combine DataFrames [OK]
Hint: Use pd.concat() to join DataFrames vertically [OK]
Common Mistakes:
Using + or & operators on DataFrames
Using deprecated append() method
Trying to print head() and tail() separately without combining
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.