Master Pandas From Scratch

Learn Pandas fundamentals, Series, DataFrames, data selection, filtering, cleaning, sorting, grouping, merging, reshaping, string operations, time series, data analysis, performance optimization, visualization, and practical projects through structured lessons.

COURSE SYLLABUS
1. Pandas Introduction and Environment Setup
Introduction to Pandas Installation and Environment Setup
2. Pandas Core Data Structures
Data Structures Overview Pandas Series DataFrames Indexes, Rows and Columns Shape, Size and Dimensions
3. Pandas Data Input and Output
Data Input and Output Overview Reading CSV, Excel and JSON Files Writing and Exporting Files
4. Initial Data Inspection
Data Inspection Overview Head and Tail Data Info and Types Descriptive Statistics
5. Selection and Indexing
Selection and Indexing Overview Column Selection loc and iloc Index Management
6. Filtering and Conditional Selection
Filtering Overview Boolean Filtering Multiple Conditions isin, between and str.contains
7. Basic Data Cleaning
Data Cleaning Overview Missing Values Duplicate Data Rename and Replace Data Type Conversion
8. Vectorized Operations and Sorting
Operations and Sorting Overview Column Operations Sorting Values and Indexes Sum, Mean, Max and Min
9. GroupBy and Aggregation
GroupBy Overview Grouping Data Aggregation Functions Group Filtering Group Transform
10. Merging, Joining and Concatenating
Merge, Join and Concat Overview Concatenation Merging DataFrames Joining by Index
11. Reshaping and Pivoting
Reshaping Overview Pivot Tables Melting Data Stack and Unstack Cross Tabulation
12. String and Categorical Data
String and Categorical Overview String Operations Categorical Data One-Hot Encoding
13. Time Series Analysis
Time Series Overview DateTime Operations Time-Based Indexes Resampling Data Shift and Rolling Windows
14. Custom Functions and Mapping
Apply and Mapping Overview Apply and Lambda Functions Map Operations Element-Wise Operations
15. Performance and Large Datasets
Performance Overview Memory Optimization Processing Files in Chunks PyArrow, Dask and Polars
16. Pandas Data Visualization
Visualization Overview Basic Plotting Histograms, Lines, Scatter and Box Plots Matplotlib and Seaborn

Head and Tail in Pandas

Learn how to preview the first and last rows of a DataFrame using Pandas.

1. Introduction to head() and tail()

When working with a dataset, you often need to examine a few records before performing calculations or analysis. Printing every row is not practical for large datasets.

Pandas provides two useful methods for this purpose: head() and tail().

head()

Displays the first rows of a DataFrame or Series.

tail()

Displays the last rows of a DataFrame or Series.

Remember: Both methods display five rows by default. You can pass a number to request a different number of rows.

2. Create a Sample DataFrame

We will use student records to understand both methods.

import pandas as pd

data = {
    "Name": [
        "Samadhan",
        "Pradnya",
        "Samiksha",
        "Aarav",
        "Neha",
        "Rahul",
        "Priya",
        "Amit"
    ],
    "Marks": [85, 92, 88, 76, 95, 81, 90, 78],
    "City": [
        "Pune",
        "Mumbai",
        "Nashik",
        "Pune",
        "Mumbai",
        "Nashik",
        "Pune",
        "Mumbai"
    ]
}

df = pd.DataFrame(data)

print(df)

Output

       Name  Marks    City
0  Samadhan     85    Pune
1   Pradnya     92  Mumbai
2  Samiksha     88  Nashik
3     Aarav     76    Pune
4      Neha     95  Mumbai
5     Rahul     81  Nashik
6     Priya     90    Pune
7      Amit     78  Mumbai

This DataFrame contains eight records. The default index runs from 0 to 7.

3. Using head()

The head() method returns the first five rows when called without an argument.

print(df.head())

Output

       Name  Marks    City
0  Samadhan     85    Pune
1   Pradnya     92  Mumbai
2  Samiksha     88  Nashik
3     Aarav     76    Pune
4      Neha     95  Mumbai

Explanation

  • Only the first five rows are displayed.
  • The original row indexes are preserved.
  • The original DataFrame is not modified.
  • The remaining rows are not included in this preview.

Display the first three rows

print(df.head(3))

Output

       Name  Marks    City
0  Samadhan     85    Pune
1   Pradnya     92  Mumbai
2  Samiksha     88  Nashik

Here, the argument 3 tells Pandas to return only the first three rows.

4. Image 1: How head() Selects Rows

The highlighted rows in this illustration represent the rows returned by df.head(3).

df.head(3) Index Name Marks City 0 Samadhan 85 Pune 1 Pradnya 92 Mumbai 2 Samiksha 88 Nashik 3 Aarav 76 Pune 4 Neha 95 Mumbai Green rows = first three records returned

Image 1: head(3) returns rows at the beginning of the DataFrame.

5. Using tail()

The tail() method returns the last five rows by default. It is useful for checking the end of a dataset or reviewing recently appended records.

print(df.tail())

Output

       Name  Marks    City
3     Aarav     76    Pune
4      Neha     95  Mumbai
5     Rahul     81  Nashik
6      Priya     90    Pune
7       Amit     78  Mumbai

Display the last three rows

print(df.tail(3))

Output

    Name  Marks    City
5  Rahul     81  Nashik
6  Priya     90    Pune
7   Amit     78  Mumbai

Explanation

  • tail(3) returns the last three rows.
  • The records appear in their original order.
  • The original index labels remain unchanged.
  • The original DataFrame is not modified.

6. Image 2: How tail() Selects Rows

The highlighted rows below represent the last three rows returned by df.tail(3).

df.tail(3) Index Name Marks City 0 Samadhan 85 Pune 1 Pradnya 92 Mumbai 2 Samiksha 88 Nashik 5 Rahul 81 Nashik 6 Priya 90 Pune Orange rows illustrate records near the end

Image 2: tail() returns records from the end of the DataFrame.

Note: This illustration is a visual guide. In the sample dataset, df.tail(3) returns index labels 5, 6, and 7: Rahul, Priya, and Amit.

7. Difference Between head() and tail()

Feature head() tail()
Purpose Displays rows from the beginning. Displays rows from the end.
Default row count 5 5
Custom count df.head(3) df.tail(3)
Common use Check initial records. Check final records.
Changes original DataFrame? No No
# First two rows
print(df.head(2))

# Last two rows
print(df.tail(2))

8. What Happens When You Change the Number?

The number passed to head() or tail() controls how many rows are requested.

print(df.head(1))
print(df.tail(1))

print(df.head(10))
print(df.tail(10))

Since the DataFrame contains eight rows, requesting ten rows returns all available rows. These methods do not create extra records to reach the requested number.

Tip: You can also use a negative number. For example, df.head(-2) returns all but the last two rows, while df.tail(-2) returns all but the first two rows.

9. Using head() and tail() with a Series

These methods are also available on a Pandas Series. A Series represents a single labeled sequence of values.

marks = df["Marks"]

print(marks.head(3))
print(marks.tail(3))

Output of marks.head(3)

0    85
1    92
2    88
Name: Marks, dtype: int64

Output of marks.tail(3)

5    81
6    90
7    78
Name: Marks, dtype: int64

10. Practical Example: Inspect a CSV File

When loading a CSV file, it is useful to preview the records immediately to check whether the file was read as expected.

import pandas as pd

df = pd.read_csv("students.csv")

print("First records:")
print(df.head())

print("Last records:")
print(df.tail())

This code assumes that students.csv exists in the current working directory. If it is stored elsewhere, provide the correct file path.

Recommended workflow: Load the file, inspect the first rows, inspect the last rows, and then check df.shape and df.info().

11. Common Mistakes

Mistake 1: Forgetting the parentheses

# Incorrect: refers to the method itself
print(df.head)

# Correct: calls the method
print(df.head())

Mistake 2: Assuming the default is all rows

df.head() and df.tail() normally return up to five rows, not the complete dataset.

Mistake 3: Assuming the original data is modified

These methods return a preview. They do not delete the remaining rows from the original DataFrame.

Mistake 4: Confusing row count with index labels

The index labels shown in the output identify the rows. They do not mean that the returned preview starts at index 0.

12. Hands-on Practice Exercise

Samiksha is analyzing a dataset containing student records. Use the following DataFrame to complete the exercises.

import pandas as pd

df = pd.DataFrame({
    "Name": ["Samadhan", "Pradnya", "Samiksha", "Aarav", "Neha", "Rahul"],
    "Marks": [85, 92, 88, 76, 95, 81],
    "City": ["Pune", "Mumbai", "Nashik", "Pune", "Mumbai", "Nashik"]
})

Tasks

  1. Display the first five rows.
  2. Display the first two rows.
  3. Display the last three rows.
  4. Display only the first row.
  5. Display the first ten requested rows and observe the result.
  6. Extract the Marks column and display its last two values.

Suggested solution

print(df.head())
print(df.head(2))
print(df.tail(3))
print(df.head(1))
print(df.head(10))

marks = df["Marks"]
print(marks.tail(2))

13. Revision Questions

  1. What is the purpose of head()?
  2. What is the purpose of tail()?
  3. How many rows do both methods return by default?
  4. How do you display the first three rows?
  5. How do you display the last four rows?
  6. Do these methods modify the original DataFrame?
  7. Can you use head() and tail() on a Series?
  8. What happens when you request more rows than the dataset contains?

Summary

Pandas provides head() and tail() to preview records at the beginning and end of a DataFrame or Series. Both methods return five rows by default, and you can pass a number to control the preview size.

Use head() to inspect initial records and tail() to inspect final records. These methods return a preview without modifying the original DataFrame.

After mastering row previews, continue to DataFrame structure, data types, and descriptive statistics.