CS Pathfinder Logo CS Pathfinder

Unit 5 · Advanced Topics

Introduction to Pandas

Pandas is a powerful open-source data manipulation and analysis library for Python, providing two primary data structures — Series and DataFrame — that make working with structured data fast, intuitive, and expressive.

Table of Contents

University Definition

Pandas (Panel Data) is an open-source Python library built on top of NumPy that provides high-performance, easy-to-use data structures and data analysis tools. Its two main structures are Series (1-dimensional) and DataFrame (2-dimensional tabular data).

Installation & Import

# Install (in terminal)
pip install pandas

# Import
import pandas as pd
import numpy as np

1. Series

A Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floats, etc.). Think of it as a single column of a spreadsheet.

import pandas as pd

# Creating Series from list
s = pd.Series([10, 20, 30, 40])
print(s)

# Output:
# 0    10
# 1    20
# 2    30
# 3    40
# dtype: int64

# Creating Series with custom index
s2 = pd.Series([85, 90, 78],
               index=["Math", "Science", "English"])
print(s2)

# Output:
# Math       85
# Science    90
# English    78
# dtype: int64

# Access by label
print(s2["Math"])       # 85
print(s2.mean())         # 84.33

2. DataFrame

A DataFrame is a two-dimensional labeled data structure with columns of potentially different types. Think of it as a spreadsheet, SQL table, or a dictionary of Series objects.

Creating DataFrame from Dictionary

import pandas as pd

data = {
    "Name":   ["Amit", "Priya", "Rahul", "Sneha"],
    "Age":    [22, 21, 23, 22],
    "Marks":  [85, 92, 78, 95],
    "Grade":  ["A", "A+", "B+", "A+"]
}

df = pd.DataFrame(data)
print(df)

# Output:
#     Name  Age  Marks Grade
# 0   Amit   22     85     A
# 1  Priya   21     92    A+
# 2  Rahul   23     78    B+
# 3  Sneha   22     95    A+

Creating DataFrame from Lists

# From list of lists
rows = [
    ["Amit", 22, 85],
    ["Priya", 21, 92],
    ["Rahul", 23, 78]
]
df2 = pd.DataFrame(rows, columns=["Name", "Age", "Marks"])
print(df2)

Creating DataFrame from NumPy Array

import numpy as np

arr = np.random.randint(50, 100, size=(3, 3))
df3 = pd.DataFrame(arr,
    columns=["Math", "Science", "English"],
    index=["A", "B", "C"])
print(df3)

3. Reading Data from CSV

# Read CSV file
df = pd.read_csv("students.csv")

# Read first few rows
print(df.head())

# Read specific number of rows
print(df.head(10))

# Save DataFrame to CSV
df.to_csv("output.csv", index=False)

4. Inspecting Data

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

print(df.head())       # First 5 rows
print(df.tail())       # Last 5 rows
print(df.shape)        # (rows, columns)
print(df.columns)      # Column names
print(df.dtypes)       # Data types of each column
print(df.info())       # Summary: non-null counts, types
print(df.describe())   # Statistics: count, mean, std, min, max

5. Selecting Data

data = {
    "Name":   ["Amit", "Priya", "Rahul"],
    "Age":    [22, 21, 23],
    "Marks":  [85, 92, 78]
}
df = pd.DataFrame(data, index=["r1", "r2", "r3"])

# Select column by name
print(df["Name"])

# Select multiple columns
print(df[["Name", "Marks"]])

# iloc — select by integer position
print(df.iloc[0])         # First row
print(df.iloc[0:2])       # First two rows
print(df.iloc[0, 1])     # Row 0, Col 1 → 22

# loc — select by label
print(df.loc["r1"])       # Row with label "r1"
print(df.loc["r1":"r2", "Name":"Marks"])  # Rows r1-r2, cols Name-Marks
Selector Based On Example
df["col"]Column namedf["Name"]
df.iloc[]Integer positiondf.iloc[0, 1]
df.loc[]Label / indexdf.loc["r1", "Name"]

6. Filtering Data

data = {
    "Name":   ["Amit", "Priya", "Rahul", "Sneha"],
    "Marks":  [85, 92, 78, 95]
}
df = pd.DataFrame(data)

# Filter rows where marks > 80
result = df[df["Marks"] > 80]
print(result)

# Output:
#     Name  Marks
# 0   Amit     85
# 1  Priya     92
# 3  Sneha     95

# Multiple conditions
result2 = df[(df["Marks"] > 80) & (df["Marks"] < 95)]
print(result2)

# Sorting
sorted_df = df.sort_values("Marks", ascending=False)
print(sorted_df)

# Basic statistics
print(df["Marks"].mean())   # 87.5
print(df["Marks"].max())    # 95
print(df["Marks"].min())    # 78

Practical Example: Student Records Analysis

import pandas as pd

# Create student DataFrame
students = pd.DataFrame({
    "Name":   ["Amit", "Priya", "Rahul", "Sneha", "Vikram"],
    "Math":   [85, 92, 78, 95, 88],
    "Science": [80, 88, 72, 91, 85],
    "English": [75, 90, 85, 88, 80]
})

# Add Total column
students["Total"] = students["Math"] + students["Science"] + students["English"]

# Add Average column
students["Average"] = students["Total"] / 3

# Print full table
print(students)

# Topper
topper = students[students["Average"] == students["Average"].max()]
print(f"\nTopper:\n{topper}")

# Class average
print(f"\nClass Average: {students['Average'].mean():.2f}")

University Exam Tip

University exams often ask: "What is DataFrame?", "Difference between iloc and loc", or "Write a program to read CSV and display statistics". Always include code with output. Mention head(), info(), describe(), and filtering with conditions.

Key Points

Series = 1D labeled array; DataFrame = 2D labeled table.

pd.read_csv() reads CSV files; df.to_csv() saves DataFrames.

head(), tail(), info(), describe() are used to inspect data.

iloc[] selects by position; loc[] selects by label.

Filtering uses boolean conditions: df[df["col"] > value].

Pandas is built on top of NumPy and is essential for data analysis.

Practice Questions

  1. Create a DataFrame of 5 students with Name, Roll No, and Marks. Display the topper.
  2. Read a CSV file and use describe() to show statistics.
  3. Difference between iloc and loc with examples.
  4. Filter all students with marks above 80 and sort them in descending order.
  5. Write a program to add a new column "Grade" based on marks (>= 90: A+, >= 80: A, else: B).

Summary

Pandas provides essential tools for data manipulation in Python. Series and DataFrame are the core structures. Master reading CSV, inspecting data, selecting with iloc/loc, filtering, sorting, and basic statistics for exams and real-world data analysis.

Python Programming Handwritten Notes

Master Python Programming with Easy Handwritten Notes – Perfect for Interviews, Placements, GATE & Exams.