Python Foundations for Analysts
Phase 4 goal
Write Python that reads messy program data, transforms it with pandas, and generates finished Excel and Word deliverables (the same kind of document automation your coworker builds), reliably and repeatably.
Environment note: Use your organization's approved Python distribution if one exists. Some environments require specific installers, package mirrors, or a pre-approved package list. Packages used:
pandas,openpyxl,python-docx,matplotlib(and optionallydocxtpl). Work only with the fictional dataset while learning.
π― Learning objectives
Tick each one when you can actually do it.
π‘ Key concepts
- Setup:
python -m venv .venvβ activate βpip install pandas openpyxl python-docx matplotlib. Keep arequirements.txt. - Tools: VS Code with the Python extension. Jupyter notebooks for exploration,
.pyscripts for repeatable automation. - Core syntax mapped to Excel thinking:
Excel idea Python Cell value variable cpi = 0.93Range / column list [0.93, 1.02, 0.88]Lookup table dict {"PRG-001": "KESTREL", ...}IF / IFS if / elif / elseFill down a formula forloop or list comprehensionLAMBDA deffunction - f-strings:
f"{program}: CPI {cpi:.2f}, CV ${cv:,.0f}" - pathlib for file paths:
Path("Data") / "milestones.csv" - Errors: read tracebacks bottom-up.
try/exceptfor expected failures (missing file).
π Hands-on exercise: EVM Functions in Python
Data: a hard-coded list of dicts for 5 programs: {"program": "PRG-001", "bcwp": 4_200_000, "acwp": 4_650_000, "bcws": 4_500_000, "bac": 18_000_000}.
- Write functions
cpi(ev, ac),spi(ev, pv),eac_cpi(bac, ev, ac),rag(idx)that handle divide-by-zero (returnNone). - Loop over programs and print a formatted line per program.
- Write the results to a CSV with the
csvmodule. - Wrap the file write in try/except and print a helpful message if the folder doesn't exist.
π¦ Mini-project: Milestone Countdown Script
A script that reads milestones.csv with the csv module (no pandas yet), finds incomplete milestones forecast within the next 60 days, sorts them by date, and prints a clean text report grouped by program, with days remaining and slip vs. baseline.
π§ Self-check quiz
Answer in your own words, then check. Grade yourself honestly; your score is saved.
f"{x:,.0f}" produce for x = 1234567.89?1,234,568