📖 Scenario: You work as a data analyst for a small bookstore. You have a list of books with their titles, authors, and prices. You want to quickly see the first few and last few books in your list to understand the data better.
🎯 Goal: Learn how to use the head() and tail() methods in pandas to view the first and last rows of a dataset.
📋 What You'll Learn
Create a pandas DataFrame with book data
Use head() to see the first 3 rows
Use tail() to see the last 2 rows
Print the results
💡 Why This Matters
🌍 Real World
Data analysts often need to quickly check the start and end of datasets to understand their structure and spot any issues.
💼 Career
Knowing how to use <code>head()</code> and <code>tail()</code> is a basic but essential skill for data scientists and analysts working with pandas.
Progress0 / 4 steps
1
Create the book data DataFrame
Import pandas as pd and create a DataFrame called books with these exact columns and rows: Title: 'The Alchemist', '1984', 'To Kill a Mockingbird', 'The Great Gatsby', 'Moby Dick' Author: 'Paulo Coelho', 'George Orwell', 'Harper Lee', 'F. Scott Fitzgerald', 'Herman Melville' Price: 10.99, 8.99, 7.99, 6.99, 9.99
Data Analysis Python
Hint
Use pd.DataFrame with a dictionary where keys are column names and values are lists of column data.
2
Set the number of rows to view
Create two variables: num_head set to 3 and num_tail set to 2. These will control how many rows to see from the start and end of the DataFrame.
Data Analysis Python
Hint
Just assign the numbers 3 and 2 to the variables num_head and num_tail.
3
Use head() and tail() to get rows
Create two new variables: first_books using books.head(num_head) and last_books using books.tail(num_tail) to get the first and last rows.
Data Analysis Python
Hint
Use head() and tail() methods with the variables num_head and num_tail.
4
Print the first and last books
Print the variables first_books and last_books to see the first 3 and last 2 rows of the DataFrame.
Data Analysis Python
Hint
Use print() to show the DataFrames first_books and last_books.
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.