Stop 06 · Python for Data AnalysisTopics · 8/12

NumPy Basics

Part of the Data Analyst Roadmap

In short

Pandas runs on NumPy underneath. You won't write much NumPy directly, but knowing how it thinks explains why vectorised code is fast and loops over rows are slow.

The numerical computing library Pandas is built on top of — arrays, vectorized math operations, and basic statistical functions, useful directly and as a foundation for understanding Pandas internals.

The jaadu rolls paint across a full tray of eggs at once.

Think of it like this

Painting a whole egg tray

An egg tray has fixed slots, all holding the same kind of thing. To colour them, you don't pick up each egg. You roll paint across the whole tray at once. NumPy arrays are trays of same-type numbers, and maths runs on all of them together.

Watch it happen

One operation, every element

100
0
250
1
80
2
400
3

1/4prices = np.array([100, 250, 80, 400]). One type, fixed size, packed tightly in memory.

The big ideas

What you need to know

ndarray

A grid of numbers, all the same type. .shape tells you its size, like (4,) or (100, 3).

Vectorised maths

arr * 2 or arr + other_arr works element by element, in fast compiled code, without a Python loop.

Boolean masks

arr[arr > 100] keeps matching values. Pandas filters work exactly the same way.

Handy helpers

np.where for if-else on arrays, np.random for simulations, np.percentile for quick summaries.

See it in code

Walk through the code

python
1import numpy as np2 3prices = np.array([100, 250, 80, 400])4with_gst = prices * 1.185print(with_gst.round(2))6 7expensive = prices[prices > 100]8print(expensive)9 10label = np.where(prices > 200, "premium", "regular")11print(label, prices.mean(), np.percentile(prices, 90))

1/3Multiply the whole array at once. Every price gets 18% added.

Vectorised maths, a boolean mask and np.where. These same ideas power every fast Pandas operation.

Save yourself the pain

Common mistakes

Oops

Looping over a DataFrame with for row in df.iterrows() to add a column.

Do this instead

Use vectorised maths like df["a"] * 1.18 or np.where. It's usually far faster and shorter.

Oops

Putting text and numbers in one array and wondering why maths fails.

Do this instead

Arrays hold one type. Mixed values become text. Keep numbers in their own array or column.

Your mission

Feel the speed

~15 min

Quick check

a = np.array([2, 4, 6]). What is a * 2?

Go deeper

  1. 1Practice basic array creation and vectorized operations (no explicit loops).
  2. 2Learn why vectorized NumPy operations are much faster than Python loops.
  3. 3Use NumPy's mean, std, and percentile functions on a sample array.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge