Project 10: CSV Story Explorer 📊¶
Difficulty: 🟢 Beginner
Run cells top to bottom. Each step builds on the previous one and saves real files under outputs/csv_story_explorer/.
Description¶
Create a dataset, analyze it with pandas, and render a chart — your first data storytelling pipeline.
Libraries & Modules¶
pandas—DataFrame,read_csvmatplotlib.pyplot- Built-in:
csv,random
Python Concepts You'll Practice¶
CSV files, DataFrames, basic plotting, aggregation
🎛️ Parameter Variations¶
NUM_STUDENTS— 10 vs 100 rowsCHART_TYPE—'bar','scatter','hist'SUBJECT— column to visualizeCOLOR— matplotlib color name or hex
In [1]:
# Install dependencies for this project (safe to re-run)
import sys
!{sys.executable} -m pip install pillow numpy matplotlib wordcloud qrcode[pil] python-barcode fpdf2 jinja2 folium plotly pandas scipy ipywidgets -q
Skipping pip install (already installed)
In [2]:
# Interactive Jupyter setup — run this cell first
%matplotlib inline
from pathlib import Path
from IPython.display import display, Image as IPImage, HTML, Markdown, Audio, IFrame
OUTPUT_DIR = Path("outputs") / "csv_story_explorer"
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
print(f"✅ Outputs folder: {OUTPUT_DIR.resolve()}")
✅ Outputs folder: C:\Users\Hansel Yan\Projects\CodeItAll\outputs\csv_story_explorer
Step 1 — Generate sample CSV data¶
In [3]:
import csv
import random
import pandas as pd
# 🎛️ TWEAK THESE
NUM_STUDENTS = 30
SUBJECTS = ["Python", "Math", "Art", "Music", "Science"]
csv_path = OUTPUT_DIR / "student_scores.csv"
rows = [["name", "subject", "score"]]
names = [f"Student_{i:02d}" for i in range(1, NUM_STUDENTS + 1)]
for name in names:
subject = random.choice(SUBJECTS)
score = random.randint(55, 100)
rows.append([name, subject, score])
with open(csv_path, "w", newline="", encoding="utf-8") as f:
csv.writer(f).writerows(rows)
df = pd.read_csv(csv_path)
df.head(10)
Out[3]:
| name | subject | score | |
|---|---|---|---|
| 0 | Student_01 | Python | 81 |
| 1 | Student_02 | Art | 61 |
| 2 | Student_03 | Math | 97 |
| 3 | Student_04 | Math | 65 |
| 4 | Student_05 | Art | 85 |
| 5 | Student_06 | Science | 67 |
| 6 | Student_07 | Python | 56 |
| 7 | Student_08 | Math | 72 |
| 8 | Student_09 | Math | 84 |
| 9 | Student_10 | Music | 60 |
Step 2 — Analyze and visualize¶
In [4]:
import matplotlib.pyplot as plt
# 🎛️ TWEAK THESE
CHART_TYPE = "bar" # "bar" | "scatter" | "hist"
SUBJECT_FILTER = None # e.g. "Python" or None for all
COLOR = "#6c5ce7"
plot_df = df if SUBJECT_FILTER is None else df[df["subject"] == SUBJECT_FILTER]
avg_by_subject = plot_df.groupby("subject")["score"].mean()
fig, ax = plt.subplots(figsize=(10, 5))
if CHART_TYPE == "bar":
avg_by_subject.plot(kind="bar", ax=ax, color=COLOR, edgecolor="white")
ax.set_ylabel("Average Score")
elif CHART_TYPE == "scatter":
ax.scatter(range(len(plot_df)), plot_df["score"], c=COLOR, alpha=0.7)
ax.set_ylabel("Score")
else:
ax.hist(plot_df["score"], bins=10, color=COLOR, edgecolor="white")
ax.set_xlabel("Score")
ax.set_title("CSV Story Explorer — CodeItAll")
plt.xticks(rotation=45, ha="right")
plt.tight_layout()
out = OUTPUT_DIR / "csv_story_chart.png"
fig.savefig(out, dpi=120)
plt.show()
print(f"Chart saved to {out}")
Chart saved to outputs\csv_story_explorer\csv_story_chart.png
✅ Project complete¶
Check your output folder: outputs/csv_story_explorer/
Next: Project 11: Fractal Tree Garden · Index