Python Series track

Week 08 · Advanced Track

Week 8 — NumPy & Pandas

Accelerate numerical computation with NumPy arrays and explore tabular data with Pandas. Combine them with Matplotlib to visualize results.

Advanced Track2 min readFree lesson

Level: Advanced | Prerequisite: Week 7

Learning Goals

  • Creating, operating, and indexing NumPy arrays
  • Basics of Pandas DataFrame
  • Reading CSV, filtering, aggregation
  • groupby and pivot tables
pip install numpy pandas matplotlib

1. Basics of NumPy

import numpy as np

a = np.array([1, 2, 3, 4, 5])
print(a.mean(), a.std(), a.sum())

#Vector operations (much faster than Python list)
print(a * 2)        # [2 4 6 8 10]
print(a + a)
print(a > 2)        #Boolean array

# 2D
m = np.arange(12).reshape(3, 4)
print(m)
print(m.shape)
print(m.sum(axis=0))   #Column sum
print(m.sum(axis=1))   #Row sum

2. NumPy Indexing

a = np.arange(10)
print(a[3])
print(a[2:6])
print(a[a > 5])      #Boolean Indexing

m = np.arange(12).reshape(3, 4)
print(m[1, 2])       #Row 1, Column 2
print(m[:, 1])       #All rows, Column 1
print(m[1, :])       #Row 1

3. Pandas DataFrame

import pandas as pd

df = pd.DataFrame({
    "name": ["Alice", "Bob", "Charlie", "Dave"],
    "score": [92, 78, 85, 67],
    "dept": ["A", "B", "A", "B"],
})

print(df)
print(df.describe())
print(df.shape, df.columns.tolist())

4. Filtering, Sorting, Aggregation

#Filter
print(df[df.score >= 80])

#Sort
print(df.sort_values("score", ascending=False))

#Group Aggregate
print(df.groupby("dept")["score"].mean())
print(df.groupby("dept").agg({"score": ["mean", "max", "count"]}))

5. Reading CSV

df = pd.read_csv("sales.csv")
df["date"] = pd.to_datetime(df["date"])
monthly = df.groupby(df["date"].dt.to_period("M"))["amount"].sum()
print(monthly)

6. Visualization (matplotlib)

import matplotlib.pyplot as plt

monthly.plot(kind="bar")
plt.title("Monthly Sales")
plt.tight_layout()
plt.savefig("monthly.png")

Key Examples (examples/)

File Content
01_numpy_basic.py Arrays, Operations
02_numpy_indexing.py Indexing, Slicing
03_pandas_basic.py Creating and Accessing DataFrame
04_pandas_groupby.py Filtering, groupby

⚠️ Common Mistakes

  1. Processing NumPy arrays with for loop — Vector operations are faster
  2. SettingWithCopyWarning — Use .loc[] instead of df[df.x > 0]["y"] = 5
  3. Confusion between index vs column — df["a"] is column, df.loc[a] is index
  4. Ignoring NaN — Explicit handling with dropna(), fillna()

❓ FAQ

Q1. Difference between NumPy and list? A. NumPy: same type, contiguous memory, vector operations. Over 100x faster for numerical computations.

Q2. Difference between Pandas and SQL? A. Pandas is an in-memory DataFrame. SQL is for persistent storage + large data. Use Pandas for analysis, SQL for operational data.

Q3. I heard polars is faster? A. Correct. It's better for large data. However, the ecosystem is overwhelmingly in favor of pandas.

📝 Exercises (exercises/)

  • exercise1.md — students.csv statistics by department
  • exercise2.md — NumPy simulation (BMI for 1000 people)
  • exercise3.md — Sales pivot table

Next Week

Week 9. Packaging and Distribution

Example files

The runnable files used in this lesson. Copy them into your own project and experiment.

examples/01_numpy_basic.pyPython
import numpy as np

a = np.array([1, 2, 3, 4, 5])
print("array:", a)
print("mean:", a.mean(), "std:", a.std(), "sum:", a.sum())

#Vector Operations
print("a * 2:", a * 2)
print("a + a:", a + a)
print("a > 2:", a > 2)

# 2D
m = np.arange(12).reshape(3, 4)
print(m)
print("shape:", m.shape)
print("Sum along axis=0:", m.sum(axis=0))
print("Sum along axis=1:", m.sum(axis=1))

#Statistics
print("min/max/median:", m.min(), m.max(), np.median(m))
examples/02_numpy_indexing.pyPython
import numpy as np

a = np.arange(10)
print(a[3])
print(a[2:6])

#Boolean Indexing
print("> 5:", a[a > 5])

# 2D
m = np.arange(12).reshape(3, 4)
print(m)
print("(1, 2):", m[1, 2])
print("column 1:", m[:, 1])
print("row 1:", m[1, :])

#fancy indexing
print(m[[0, 2], [1, 3]])   #positions (0,1), (2,3)

#conditional change
m_copy = m.copy()
m_copy[m_copy > 5] = -1
print(m_copy)
examples/03_pandas_basic.pyPython
import pandas as pd

df = pd.DataFrame({
    "name": ["Alice", "Bob", "Charlie", "Dave", "Eve"],
    "score": [92, 78, 85, 67, 94],
    "dept": ["A", "B", "A", "B", "A"],
})

print(df)
print()
print("describe():")
print(df.describe())
print()
print("shape:", df.shape)
print("columns:", df.columns.tolist())
print("dtypes:")
print(df.dtypes)

#Select row/column
print("Score column:")
print(df["score"])
print("Row 0:")
print(df.iloc[0])
examples/04_pandas_groupby.pyPython
import pandas as pd

df = pd.DataFrame({
    "name": ["Alice", "Bob", "Charlie", "Dave", "Eve"],
    "score": [92, 78, 85, 67, 94],
    "dept": ["A", "B", "A", "B", "A"],
})

#Filter
print("80 points or more:")
print(df[df.score >= 80])

#Sort
print("Descending order of scores:")
print(df.sort_values("score", ascending=False))

# groupby
print("Average by department:")
print(df.groupby("dept")["score"].mean())

print("Various statistics by department:")
print(df.groupby("dept").agg(
    avg=("score", "mean"),
    high=("score", "max"),
    n=("score", "count"),
))

Assignment

Input

name,dept,score
Alice,A,92
Bob,B,78
Charlie,A,85
Dave,B,67
Eve,A,94
Frank,C,71

Output

[Average by Department]
A    90.33
B    72.50
C    71.00

[Overall Statistics]
Average: 81.17
Highest: Eve (94)

Assignment 2. NumPy simulation (BMI for 1000 people)

Goal

  • Heights of 1000 people (Normal distribution: mean 170cm, standard deviation 8)
  • Weights of 1000 people (Normal distribution: mean 65kg, standard deviation 10)
  • Calculate BMI
  • Print number of people by category (Underweight/Normal/Overweight/Obese)

Hint

heights = np.random.normal(170, 8, 1000)
weights = np.random.normal(65, 10, 1000)
bmi = weights / (heights / 100) ** 2

Assignment 3. Sales pivot table

Input

date,category,amount
2026-01-15,food,12000
2026-01-20,drink,5000
2026-02-03,food,8500
2026-02-10,daily,3200
...

Output (Pivot)

category    daily  drink   food
date
2026-01         0   5000  12000
2026-02      3200      0   8500

Hint

df["date"] = pd.to_datetime(df["date"]).dt.to_period("M")
pivot = df.pivot_table(index="date", columns="category",
                      values="amount", aggfunc="sum", fill_value=0)
Show the reference solution

Try the assignment yourself first — comparing your attempt with a working solution is where most of the learning happens.

solution1.pyPython
import pandas as pd
from io import StringIO

CSV = """name,dept,score
Alice,A,92
Bob,B,78
Charlie,A,85
Dave,B,67
Eve,A,94
Frank,C,71
"""

df = pd.read_csv(StringIO(CSV))

print("[Average by Department]")
print(df.groupby("dept")["score"].mean())

print(f"[Overall Statistics]")
print(f"Average: {df.score.mean():.2f}")
top_idx = df.score.idxmax()
print(f"Top: {df.loc[top_idx, 'name']} ({df.loc[top_idx, 'score']})")
solution2.pyPython
import numpy as np

np.random.seed(42)

heights = np.random.normal(170, 8, 1000)
weights = np.random.normal(65, 10, 1000)
bmi = weights / (heights / 100) ** 2

print(f"Average BMI: {bmi.mean():.2f}")
print(f"Range: {bmi.min():.1f} ~ {bmi.max():.1f}")
print()

#Category
underweight = (bmi < 18.5).sum()
normal = ((bmi >= 18.5) & (bmi < 23)).sum()
overweight = ((bmi >= 23) & (bmi < 25)).sum()
obese = (bmi >= 25).sum()

print(f"Underweight: {underweight} people ({underweight/10:.1f}%)")
print(f"Normal: {normal} people ({normal/10:.1f}%)")
print(f"Overweight: {overweight} people ({overweight/10:.1f}%)")
print(f"Obese: {obese} people ({obese/10:.1f}%)")
solution3.pyPython
import pandas as pd
from io import StringIO

CSV = """date,category,amount
2026-01-15,food,12000
2026-01-20,drink,5000
2026-02-03,food,8500
2026-02-10,daily,3200
2026-02-15,drink,4800
2026-03-01,food,15000
2026-03-12,daily,2700
"""

df = pd.read_csv(StringIO(CSV))
df["month"] = pd.to_datetime(df["date"]).dt.to_period("M")

pivot = df.pivot_table(
    index="month", columns="category",
    values="amount", aggfunc="sum", fill_value=0
)
print(pivot)
print("Monthly total:")
print(pivot.sum(axis=1))

Learn this lesson with a mentor

Work through the Python Series track on your own, or join a guided cohort at our Pitampura campus and online.

Call NowEnroll Now