0
0
Data Analysis Pythondata~10 mins

head() and tail() in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - head() and tail()
Start with DataFrame
Call head(n)
Select first n rows
Return subset DataFrame
The flow shows how calling head(n) or tail(n) on a DataFrame returns the first or last n rows respectively.
Execution Sample
Data Analysis Python
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 DataFrame and shows the first 3 rows with head(3) and last 2 rows with tail(2).
Execution Table
StepActionInput DataFrameMethod CalledParameter nOutput DataFrame
1Create DataFrame{'A':[1,2,3,4,5], 'B':['a','b','c','d','e']}NoneNoneFull DataFrame with 5 rows
2Call head(3)Full DataFramehead3First 3 rows: rows 0,1,2
3Call tail(2)Full DataFrametail2Last 2 rows: rows 3,4
4EndN/AN/AN/AExecution complete
💡 All requested rows returned; no more steps.
Variable Tracker
VariableStartAfter head(3)After tail(2)Final
dfN/AFull DataFrame with 5 rowsFull DataFrame with 5 rowsFull DataFrame with 5 rows
head_dfN/ADataFrame with rows 0,1,2DataFrame with rows 0,1,2DataFrame with rows 0,1,2
tail_dfN/AN/ADataFrame with rows 3,4DataFrame with rows 3,4
Key Moments - 2 Insights
Why does head(3) return rows 0,1,2 and not 1,2,3?
Because DataFrame rows are zero-indexed, so the first row is index 0. head(3) returns the first three rows starting at index 0, as shown in execution_table step 2.
What happens if n is larger than the number of rows in the DataFrame?
head() or tail() will return all rows without error. This is because they select up to n rows but do not fail if n exceeds the DataFrame size.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what rows does head(3) return?
ARows 1,2,3
BRows 2,3,4
CRows 0,1,2
DRows 3,4,5
💡 Hint
Check the Output DataFrame column in step 2 of execution_table.
At which step does tail(2) return the last two rows?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for the method called 'tail' in execution_table.
If we call head(10) on this DataFrame, what will happen?
AReturn first 10 rows, error if less than 10
BReturn all rows since DataFrame has only 5 rows
CReturn empty DataFrame
DReturn last 10 rows
💡 Hint
Refer to key_moments about behavior when n exceeds DataFrame size.
Concept Snapshot
head(n) returns the first n rows of a DataFrame.
tail(n) returns the last n rows.
If n > number of rows, all rows are returned.
Rows are zero-indexed.
Useful for quick data preview.
Full Transcript
This lesson shows how to use head() and tail() methods on a DataFrame to view the first or last few rows. The flow starts with a DataFrame, then calls head(n) or tail(n) to select rows. The execution table traces creating a DataFrame with 5 rows, then calling head(3) which returns rows 0,1,2, and tail(2) which returns rows 3,4. Variables track the DataFrame and subsets. Key moments clarify zero-based indexing and behavior when n exceeds row count. The quiz tests understanding of which rows are returned and method behavior. The snapshot summarizes usage and rules for head() and tail().