The head() function shows the first few rows of a DataFrame or Series, helping you quickly see the start of your data.
Click to reveal answer
beginner
How do you use tail() in pandas and what does it show?
tail() shows the last few rows of a DataFrame or Series, letting you quickly check the end of your data.
Click to reveal answer
beginner
By default, how many rows do head() and tail() show?
Both head() and tail() show 5 rows by default if no number is given.
Click to reveal answer
beginner
How can you see the first 10 rows of a DataFrame named df?
Use df.head(10) to see the first 10 rows.
Click to reveal answer
beginner
Why is it useful to preview data with head() and tail()?
Previewing helps you quickly understand the data structure, check for missing values, and confirm data loading without looking at the whole dataset.
Click to reveal answer
What does df.head() return by default?
AFirst 5 rows of df
BLast 5 rows of df
CFirst 10 rows of df
DAll rows of df
✗ Incorrect
head() returns the first 5 rows by default.
How do you get the last 3 rows of a DataFrame df?
Adf.end(3)
Bdf.head(3)
Cdf.last(3)
Ddf.tail(3)
✗ Incorrect
tail(3) returns the last 3 rows.
If you want to preview the start and end of your data, which two functions do you use?
Astart() and end()
Bhead() and tail()
Cfirst() and last()
Dtop() and bottom()
✗ Incorrect
Use head() for the start and tail() for the end.
What type of object do head() and tail() return?
AA plot
BA list of values
CA new DataFrame or Series with selected rows
DA summary statistic
✗ Incorrect
They return a new DataFrame or Series containing the selected rows.
Can you specify how many rows to preview with head() and tail()?
AYes, by passing a number as an argument
BNo, they always show 5 rows
COnly <code>head()</code> allows this
DOnly <code>tail()</code> allows this
✗ Incorrect
Both functions accept a number to specify how many rows to show.
Explain how and why you would use head() and tail() when starting to analyze a new dataset.
Think about what you want to learn quickly from the data.
You got /4 concepts.
Describe the default behavior of head() and tail() and how to change the number of rows they show.
Remember the default and how to customize.
You got /3 concepts.
Practice
(1/5)
1. What does the head() function do in pandas?
easy
A. Sorts the DataFrame by index
B. Shows the last few rows of the DataFrame
C. Shows the first few rows of the DataFrame
D. Deletes 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 preview data.
Step 2: Compare with other options
tail() shows the last few rows, not the first. Deleting or sorting are unrelated to head().
Final Answer:
Shows the first few rows of the DataFrame -> Option C
Quick Check:
head() = first rows [OK]
Hint: head() previews start; tail() previews end [OK]
Common Mistakes:
Confusing head() with tail()
Thinking head() deletes rows
Assuming head() sorts 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.tail[-3]
D. df.last(3)
Solution
Step 1: Recall the syntax for tail()
The correct syntax to get last rows is df.tail(n), where n is the number of rows.
Step 2: Check each option
df.tail(3) is correct. df.head(3) shows first rows. df.tail[-3] is invalid syntax. df.last(3) does not exist.
Final Answer:
df.tail(3) -> Option A
Quick Check:
Use tail(n) for last n rows [OK]
Hint: Use tail(n) to get last n rows [OK]
Common Mistakes:
Using head() instead of tail()
Using square brackets with tail()
Using non-existent last() method
3. Given the DataFrame df below:
A B
0 5 10
1 6 11
2 7 12
3 8 13
4 9 14
What will df.head(2) return?
medium
A. Rows with index 3 and 4
B. Rows with index 0 and 1
C. Rows with index 2 and 3
D. Rows with index 1 and 2
Solution
Step 1: Understand what head(2) does
head(2) returns the first 2 rows of the DataFrame, which are rows with index 0 and 1.
Step 2: Identify the rows in the DataFrame
Rows 0 and 1 have values (5,10) and (6,11) respectively, matching the first two rows.
Final Answer:
Rows with index 0 and 1 -> Option B
Quick Check:
head(2) = first 2 rows [OK]
Hint: head(n) returns first n rows [OK]
Common Mistakes:
Selecting last rows instead of first
Confusing index numbers
Assuming head() returns columns
4. You run df.tail(3) but get an error: TypeError: 'int' object is not callable. What is the most likely cause?
medium
A. You assigned a variable named tail as an integer earlier
B. You used square brackets instead of parentheses
C. The DataFrame df is empty
D. You forgot to import pandas
Solution
Step 1: Understand the error message
TypeError: 'int' object is not callable means you tried to call an integer like a function.
Step 2: Identify cause related to tail
If you assigned tail as an integer variable, it overrides the method, causing this error when calling df.tail(3).
Final Answer:
You assigned a variable named tail as an integer earlier -> Option A
Quick Check:
Variable name conflicts cause callable errors [OK]
Hint: Avoid naming variables same as methods [OK]
Common Mistakes:
Using square brackets instead of parentheses
Assuming empty DataFrame causes this error
Ignoring import errors
5. You have a DataFrame df with 1000 rows. You want to preview the first 5 rows and the last 5 rows together in one DataFrame. Which code correctly does this?
hard
A. df.head(5) + df.tail(5)
B. df.head(5).merge(df.tail(5))
C. df.head(5).append(df.tail(5))
D. pd.concat([df.head(5), df.tail(5)])
Solution
Step 1: Understand how to combine DataFrames vertically
To combine rows from two DataFrames, use pd.concat() or append(). The plus operator and merge are incorrect here.
Step 2: Check each option
pd.concat([df.head(5), df.tail(5)]) correctly stacks first and last 5 rows. df.head(5) + df.tail(5) causes error. append() is deprecated in recent pandas versions. merge() is for joining on columns, not stacking rows.
Final Answer:
pd.concat([df.head(5), df.tail(5)]) -> Option D
Quick Check:
Use pd.concat() to stack DataFrames [OK]
Hint: Use pd.concat() to combine head and tail rows [OK]
Common Mistakes:
Using + operator to add DataFrames
Using deprecated append() method
Using merge() instead of concat()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.