Challenge - 5 Problems
Head and Tail Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of head() with default parameter
What will be the output of the following code snippet?
Data Analysis Python
import pandas as pd data = {'Name': ['Anna', 'Bob', 'Charlie', 'David', 'Eva'], 'Age': [23, 35, 45, 28, 32]} df = pd.DataFrame(data) print(df.head())
Attempts:
2 left
💡 Hint
head() by default shows the first 5 rows of the DataFrame.
✗ Incorrect
The head() method without any argument returns the first 5 rows of the DataFrame. Since the DataFrame has exactly 5 rows, all rows are shown.
❓ Predict Output
intermediate2:00remaining
Output of tail() with parameter 3
What will be the output of this code?
Data Analysis Python
import pandas as pd data = {'City': ['NY', 'LA', 'Chicago', 'Houston', 'Phoenix'], 'Population': [8.4, 4.0, 2.7, 2.3, 1.7]} df = pd.DataFrame(data) print(df.tail(3))
Attempts:
2 left
💡 Hint
tail(3) shows the last 3 rows of the DataFrame.
✗ Incorrect
The tail(3) method returns the last 3 rows of the DataFrame, which are Chicago, Houston, and Phoenix.
❓ data_output
advanced2:00remaining
Number of rows returned by head() and tail() combined
Given a DataFrame with 10 rows, what is the total number of rows returned when you combine df.head(7) and df.tail(5)?
Attempts:
2 left
💡 Hint
head(7) returns first 7 rows, tail(5) returns last 5 rows. Some rows overlap.
✗ Incorrect
The first 7 rows and last 5 rows overlap on rows 3 to 7, so total unique rows are 10.
🔧 Debug
advanced2:00remaining
Identify the error in using head()
What error will this code produce and why?
import pandas as pd
data = {'A': [1,2,3,4,5]}
df = pd.DataFrame(data)
print(df.head(-3))
Data Analysis Python
import pandas as pd data = {'A': [1,2,3,4,5]} df = pd.DataFrame(data) print(df.head(-3))
Attempts:
2 left
💡 Hint
head() with a negative argument returns an empty DataFrame.
✗ Incorrect
pandas treats a negative value passed to head() as 0, so df.head(-3) returns an empty DataFrame with 0 rows. No error or exception is raised.
🚀 Application
expert3:00remaining
Using head() and tail() to sample data
You have a DataFrame with 1000 rows sorted by date ascending. You want to create a new DataFrame that contains the first 10 and last 10 rows combined without duplicates. Which code snippet correctly achieves this?
Attempts:
2 left
💡 Hint
Use concat and drop_duplicates to avoid repeated rows.
✗ Incorrect
Option D concatenates first 10 and last 10 rows and removes duplicates, ensuring no repeated rows if overlap exists.