5.4.21 · D1Scientific Computing (Python)

Foundations — Pandas — Series, DataFrame, indexing, groupby, merge, pivot

1,691 words8 min readBack to topic

Before you can read a single line of the parent note, you need the vocabulary it assumes you already own. This page builds each piece from absolute zero, in the order they depend on each other. Nothing here uses a word we haven't first drawn a picture of.


0. The most primitive object: a labelled box

The whole subject lives on the difference between the last two. Position asks "which one is it in line?". Label asks "what is it called?". Look at the picture: the same three boxes, addressed two different ways.

Figure — Pandas — Series, DataFrame, indexing, groupby, merge, pivot

1. The array — many boxes in a fixed line

An array has no name-tags. It knows "the 2nd box holds 30" but not "the box called u2 holds 30". That missing name-tag is the gap pandas fills.


2. The index — the strip of name-tags

Figure — Pandas — Series, DataFrame, indexing, groupby, merge, pivot

Two arrays stapled together — a values array and an index array of the same length — is the atom of all of pandas.


3. Series — one column with name-tags

The arrow notation just means "maps to" / "gives you". So reads "the name-tag u2 gives the value 32".


4. NaN — the shape of an empty box

Why does a missing box need its own special value instead of just 0? Because 0 is a real answer ("we measured zero sales"), while NaN means "we measured nothing at all". Confusing the two corrupts every average you compute. See the parent's alignment example, where label "z" produces NaN.


5. Alignment — matching by name-tag, not by line-position

Figure — Pandas — Series, DataFrame, indexing, groupby, merge, pivot

6. DataFrame — a wall of aligned Series

Figure — Pandas — Series, DataFrame, indexing, groupby, merge, pivot

Now you can decode the parent's one-liner: "a dictionary of aligned columns". Dictionary = column-name → column. Aligned = every column shares the same row index. That's it.


7. The three doors: [], .loc, .iloc

You now have enough to understand why there are three ways in.


8. Concepts the parent leans on (linked, not re-derived)

Concept One-line meaning Where it feeds in
Split-Apply-Combine slice into buckets → run a function per bucket → stitch back groupby
SQL Joins combine two tables on a shared key column merge
Tidy Data one row per observation, one column per variable pivot (long↔wide)
Hash Tables name→position dictionary for instant lookup why index lookup is fast

How the foundations feed the topic

value + position + label

array = NumPy row of boxes

index = strip of name-tags

hash table for fast label lookup

Series = values glued to index

NaN = empty box marker

alignment = match by label

DataFrame = wall of aligned Series

loc iloc bracket selectors

groupby split apply combine

merge join on keys

pivot long to wide

Read top to bottom: nothing on a lower row is usable until every arrow feeding it is understood. If any upstream box is fuzzy, the parent note will feel like magic.


Equipment checklist

Cover the right side and answer aloud. If you miss one, re-read its section above.

Difference between a value's position and its label
Position = how far down the line (0,1,2…); label = the name-tag glued on, which never moves when rows are reordered.
What two arrays make a Series
A values array and an index (label) array of equal length, stapled together.
What does it mean that a Series is a "partial function"
It maps label → value, but only for labels it actually contains; missing labels have no answer.
What is NaN and why not just use 0
A "Not a Number" marker meaning no value here; 0 is a real measured value, NaN means nothing was measured.
State the alignment rule in plain words
Line two Series up by label; matching labels combine, unmatched labels become NaN.
What is a DataFrame in dictionary terms
A dictionary {column label → Series} where every Series shares one common row index.
Why does .loc slice inclusively but .iloc exclusively
.loc uses labels (a name has no "stop+1", so include the named endpoint); .iloc uses positions and follows Python's exclusive-stop rule.
Why is label lookup nearly as fast as position lookup
The index builds a hash table (name→position) so finding a name-tag doesn't require scanning the whole strip.
Which foundation directly powers groupby, merge, and pivot
Alignment — each is "match by label first, then act".