1.4.6 · D3Python & Scientific Computing

Worked examples — Pandas DataFrames and Series basics

2,457 words11 min readBack to topic

This page is a drill. The parent note built the ideas: a Series is a labelled 1-D column, a DataFrame is a stack of Series that all share one row-index. Here we throw every kind of input at those ideas — clean data, missing data, mismatched labels, empty frames, the lot — and work each one to the end.

Every code block below assumes these two imports at the top of the file:

import pandas as pd
import numpy as np

pd is the nickname for pandas, np for numpy — the library that supplies np.nan, the missing-value marker.

Before line one, one reminder in plain words:

  • index = the row names (like the leftmost column in a spreadsheet you cannot type into by accident).
  • columns = the names across the top.
  • NaN = "Not a Number", Pandas' way of writing a hole where a value should be. It is a real float value that means "missing"; in code it is written np.nan.
  • dtype = the type Pandas decided a column holds: int64 (whole numbers), float64 (decimals, and the only type that can hold NaN), object (text or mixed). In code the dtype prints as the string 'int64', 'float64', etc.

Prereq refreshers if any word above feels new: 1.4.01-NumPy-arrays-and-vectorization, 1.4.02-Python-data-structures.


The scenario matrix

Every example below is tagged with the cell of this table it covers. By the last example every cell is filled.

Cell Case class What makes it tricky
A Clean construction baseline — build & read shape
B Label vs position access .loc vs .iloc disagree
C Missing keys / ragged rows holes become NaN, dtype flips
D Index misalignment in arithmetic two Series, different labels
E Boolean filter — normal + empty result condition true for none
F Degenerate inputs empty frame, single element, all-NaN
G dtype edge — int column touched by NaN silent int64 → float64
H Real-world word problem sales, group-and-average
I Exam twist df[1] vs df.iloc[1] trap

Each numbered case class maps to at least one worked example (A→Ex1, B→Ex2, C→Ex3, D→Ex4, E→Ex5, F→Ex6, G→Ex7, H→Ex8, I→Ex9).


Ex1 — Clean construction (Cell A)

Look at the figure: the dict becomes two vertical Series glued to one shared index column.

Figure — Pandas DataFrames and Series basics

Ex2 — Label vs position access (Cell B)


Ex3 — Ragged list-of-dicts → holes become NaN (Cell C)


Ex4 — Index misalignment in arithmetic (Cell D)

Figure — Pandas DataFrames and Series basics

Ex5 — Boolean filter, including the empty result (Cell E)


Ex6 — Degenerate inputs (Cell F)


Ex7 — dtype edge: int column silently becomes float (Cell G)


Ex8 — Real-world word problem (Cell H)

Related next step: turning group means into model inputs — 2.1.03-Feature-engineering-basics; plotting them — 1.5.01-Data-visualizationmatplotlib-basics; cleaning the raw table first — 1.4.07-Pandas-data-cleaningand-manipulation.


Ex9 — Exam twist: df[1] vs df.iloc[1] (Cell I)

Recall Rapid self-test

df[x] on a DataFrame selects a ___ (row/column)? ::: column Which of .loc / .iloc uses names? ::: .loc What dtype can hold NaN? ::: float64 (not int64) Adding two Series, a label present in only one gives ___? ::: NaN Mean of an all-NaN Series is ___? ::: NaN


Coverage check

Recall Every matrix cell hit?

A→Ex1, B→Ex2, C→Ex3, D→Ex4, E→Ex5, F→Ex6, G→Ex7, H→Ex8, I→Ex9. All nine cells covered ✅