█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/Programming Languages/Python (Professional)/Python for Data (pandas + NumPy)
55 minIntermediate

Python for Data (pandas + NumPy)

After this lesson, you will be able to: Use pandas and NumPy for basic data manipulation, plot results with matplotlib, and know when to reach for them vs plain Python.

Pandas + NumPy are how Python eats data, and matplotlib is how you chart it. Just enough to be dangerous + know when to escape to them.

Prerequisites:Python Scripting

NumPy basics

Array-based numeric computing. 100x faster than Python lists for math.

python
import numpy as np
a = np.array([1, 2, 3, 4, 5])
print(a * 2) # [2 4 6 8 10], vectorized; no loop
print(a.mean(), a.std(), a.sum())
# 2D array
m = np.array([[1, 2], [3, 4]])
print(m.shape) # (2, 2)
print(m @ m) # matrix multiply
# Random + seeding
rng = np.random.default_rng(seed=42)
samples = rng.normal(loc=0, scale=1, size=1000)

Pandas basics

DataFrame = a programmable spreadsheet.

python
import pandas as pd
# Load CSV
df = pd.read_csv("sales.csv", parse_dates=["date"])
# Inspect
df.head()
df.describe()
df.dtypes
# Filter + select
recent = df[df["date"] >= "2026-01-01"]
emails = df[["name", "email"]]
# Group + aggregate
by_region = df.groupby("region")["revenue"].agg(["sum", "mean", "count"])
# Join
orders = pd.read_csv("orders.csv")
merged = df.merge(orders, on="user_id", how="left")
# Write back
merged.to_csv("out.csv", index=False)
merged.to_parquet("out.parquet") # binary, smaller, faster

Plotting with matplotlib

matplotlib is the baseline plotting library. pandas plots through it directly, so a quick chart is one method call. Keep this light; the AI/ML track goes deeper into visualization.

python
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv("sales.csv", parse_dates=["date"])
# pandas plots through matplotlib: one call for a quick line chart
by_month = df.groupby(df["date"].dt.to_period("M"))["revenue"].sum()
by_month.plot(kind="line", title="Revenue by month")
plt.ylabel("Revenue (USD)")
plt.tight_layout()
plt.savefig("revenue.png", dpi=150) # save for a report
plt.show() # or display interactively
# Drop to the matplotlib API directly when you need control
fig, ax = plt.subplots(figsize=(8, 4))
ax.bar(by_region.index, by_region["sum"])
ax.set_title("Revenue by region")
ax.set_xlabel("Region")
ax.set_ylabel("Revenue (USD)")
fig.savefig("by_region.png")
# Common next steps: seaborn (prettier statistical charts on top of matplotlib),
# or plotly for interactive charts.

When to use pandas vs plain Python

Tabular data with > ~10K rows: pandas. Numeric arrays, math, ML pre-processing: NumPy. Quick charts from a DataFrame: df.plot(...) (matplotlib under the hood); save with plt.savefig for reports. Small CSV (< 1K rows), one-off scripts: csv + dataclasses might be cleaner. Streaming / large data: polars (pandas alternative, 10x faster) or DuckDB.

Common mistakes only experienced data devs avoid

Iterating with `for i, row in df.iterrows()`. SLOW. Vectorize with column ops. Using `apply()` to do what a vectorized op can. Floating CSV index column from one tool to another. Always to_csv(index=False) unless meaningful. Mixing dtypes (string in a numeric column). Pandas degrades performance.

Quick Check

When should you use pandas iterrows()?

Pick the honest answer.

Sign in and purchase access to unlock this lesson.

Sign in to purchase
←Python for Scripting and Automation
Back to Python (Professional)
Async Python (asyncio)→