Bird
Raised Fist0
Pandasdata~10 mins

head() and tail() for previewing in Pandas - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - head() and tail() for previewing
Load DataFrame
↓
Call head(n)
→Return first n rows
↓
Call tail(n)
→Return last n rows
↓
Preview data subset
You start with a DataFrame, then use head() to see the first few rows or tail() to see the last few rows for a quick preview.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({'A': range(1, 6), 'B': list('abcde')})
print(df.head(3))
print(df.tail(2))
This code creates a small DataFrame and shows the first 3 rows and last 2 rows using head() and tail().
Execution Table
StepActionInputOutput
1Create DataFrame{'A': [1,2,3,4,5], 'B': ['a','b','c','d','e']}DataFrame with 5 rows and 2 columns
2Call df.head(3)n=3Rows 0 to 2: A B 0 1 a 1 2 b 2 3 c
3Call df.tail(2)n=2Rows 3 to 4: A B 3 4 d 4 5 e
💡 All requested rows returned; preview complete
Variable Tracker
VariableStartAfter head(3)After tail(2)
dfNoneFull DataFrame with 5 rowsFull DataFrame with 5 rows
head_resultNoneDataFrame with rows 0,1,2DataFrame with rows 0,1,2
tail_resultNoneNoneDataFrame with rows 3,4
Key Moments - 2 Insights
Why does head(3) show rows 0,1,2 and not 1,2,3?
Because pandas DataFrame rows are zero-indexed, so the first row is index 0. head(3) returns the first 3 rows starting at index 0, which are rows 0,1,2 as shown in execution_table step 2.
What happens if n in head(n) or tail(n) is larger than the DataFrame size?
The function returns all rows without error. For example, if n=10 but DataFrame has 5 rows, head(10) or tail(10) returns all 5 rows. This is safe and shown by the exit_note.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what rows does df.head(3) return?
ARows 0, 1, 2
BRows 1, 2, 3
CRows 2, 3, 4
DRows 3, 4, 5
💡 Hint
Check execution_table row 2 under Output to see which rows are returned by head(3).
At which step do we get the last 2 rows of the DataFrame?
AStep 1
BStep 2
CStep 3
DNo step returns last rows
💡 Hint
Look at execution_table row 3 where tail(2) is called and returns rows 3 and 4.
If we call df.tail(10) on this DataFrame, what will happen?
AReturns last 10 rows, error if less than 10
BReturns all 5 rows without error
CReturns empty DataFrame
DReturns first 10 rows
💡 Hint
Refer to key_moments about behavior when n is larger than DataFrame size.
Concept Snapshot
head(n) returns the first n rows of a DataFrame.
tail(n) returns the last n rows.
If n is omitted, default is 5.
Useful for quick data preview without loading all rows.
Rows are zero-indexed, so first row is index 0.
Full Transcript
This lesson shows how to preview data in pandas using head() and tail(). We start with a DataFrame of 5 rows and 2 columns. Calling head(3) returns the first 3 rows, which are rows with index 0, 1, and 2. Calling tail(2) returns the last 2 rows, with index 3 and 4. These functions help us quickly see parts of the data without printing everything. If the number requested is larger than the DataFrame size, all rows are returned safely. Remember, pandas uses zero-based indexing for rows. This visual trace helps understand how head() and tail() work step-by-step.

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

  1. Step 1: Understand the purpose of head()

    The head() function is designed to display the first few rows of a DataFrame to preview data.
  2. Step 2: Compare with other options

    tail() shows the last few rows, not the first. Deleting or sorting are unrelated to head().
  3. Final Answer:

    Shows the first few rows of the DataFrame -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    df.tail(3) -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Rows with index 0 and 1 -> Option B
  4. 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

  1. Step 1: Understand the error message

    TypeError: 'int' object is not callable means you tried to call an integer like a function.
  2. 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).
  3. Final Answer:

    You assigned a variable named tail as an integer earlier -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    pd.concat([df.head(5), df.tail(5)]) -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard