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.
import pandas as pd df = pd.DataFrame({'A': range(1,6), 'B': list('abcde')}) print(df.head(3)) print(df.tail(2))
| Step | Action | Input DataFrame | Method Called | Parameter n | Output DataFrame |
|---|---|---|---|---|---|
| 1 | Create DataFrame | {'A':[1,2,3,4,5], 'B':['a','b','c','d','e']} | None | None | Full DataFrame with 5 rows |
| 2 | Call head(3) | Full DataFrame | head | 3 | First 3 rows: rows 0,1,2 |
| 3 | Call tail(2) | Full DataFrame | tail | 2 | Last 2 rows: rows 3,4 |
| 4 | End | N/A | N/A | N/A | Execution complete |
| Variable | Start | After head(3) | After tail(2) | Final |
|---|---|---|---|---|
| df | N/A | Full DataFrame with 5 rows | Full DataFrame with 5 rows | Full DataFrame with 5 rows |
| head_df | N/A | DataFrame with rows 0,1,2 | DataFrame with rows 0,1,2 | DataFrame with rows 0,1,2 |
| tail_df | N/A | N/A | DataFrame with rows 3,4 | DataFrame with rows 3,4 |
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.