Page 8 of 8~113 min topic

NumPy and pandas basics

Mastery check: your data-wrangling playbook

Every ML lab ahead starts the same way: get the data in, look at it, clean it, ask it a question. This page is that sequence, made explicit.

~14 min this pageMastery check

1Learn the idea

Read

The playbook

1. Load, then look before touching anything. pd.read_csv(...), then immediately df.shape, df.dtypes, df.head(). Every downstream bug traces back faster if you know what the data looked like at the very start.

2. Check for missing and wrong-type data early. df.isna().sum() and a scan of df.dtypes for any numeric-looking column that loaded as object — both take seconds and catch a large share of real-world data problems before they cause confusing errors three steps later.

3. Decide on missing values deliberately, not by default. Drop, fill with a sensible value, or flag for review — and be able to state in one sentence why you chose that option for this specific column, not just which pandas method you called.

4. Filter with boolean masks to answer "which rows" questions. df[condition], remembering &/| with parentheses around each condition, and .copy() once you intend to modify the filtered result.

5. Group and aggregate to answer "how does this break down" questions. groupby(...)[...].agg(...), .reset_index() when you need the result as a normal flat table again.

6. Sanity-check the final answer against the data's limits. A clean-looking summary number can still be built on a small sample, several estimated (filled) values, or a skewed group — say so alongside the answer, the same verification habit that runs through every lane of this curriculum.

Read

Why this order, specifically

Notice the order isn't arbitrary: you can't sensibly decide how to handle missing values before checking whether any exist; you can't confidently filter or group data whose types you haven't verified; and grouping numbers you haven't sanity-checked risks reporting confident conclusions built on quietly bad data. Each step depends on the one before it — which is exactly the sequence you'll see, explicitly or not, in almost every professional data-cleaning script you read from here on.

Read

Where this reappears

prediction-game and later modeling lessons in this lane assume you can load, inspect, and lightly clean a small dataset without the mechanics being new — the modeling techniques themselves are the new content there, not the data handling. ai-for-data-analysis in the Using AI lane covers the complementary skill of asking an AI assistant to do this same kind of cleaning and interpretation on your behalf, and knowing this playbook yourself is exactly what lets you verify its work rather than trusting a fluent-sounding summary blindly.

Read

Self-check

Without looking back at earlier pages, write out (even briefly) the six-step playbook above from memory, in order. If you get stuck on where filtering fits relative to missing-value handling, or forget the sanity-check step entirely, that's the part worth re-reading before your next lab — the good news is that all six steps are things you've now actually run, not just read about.

Go deeper

Before you start

Why this matters

You've now covered arrays vs. lists, vectorized math, building and inspecting DataFrames, boolean filtering, loading real CSVs with missing values, and grouping. Before moving into modeling labs (prediction-game, decision-trees, and beyond), it's worth fixing the sequence you'll reuse constantly into one repeatable playbook.

In the wild

See how this idea shows up as a product and a company — then come back to the lesson. Skills transfer across vendors.

Continue learning · glossary & guides
  1. The first thing to check right after loading a CSV is… shape, dtypes, and a quick head() / nothing, jump straight to modeling
  2. & and | in a pandas filter need… parentheses around each condition / no special handling