Filtering tables of readings

A weather station logs temperatures. Engineering work is rarely one number — it is a table: keep the hot days, convert units, report. Loops plus append are the explicit form of filter/map. pandas will later write df[df.temp >= 35]; you should be able to do it by hand first.

Weather station

Filter a table of readings.

You will be able to

  • Filter a list with if
  • Build a new list with append
  • Leave the source list unchanged

Source stays, result is new

Walk the source, decide, append to a fresh list. That pattern is the heart of data cleaning. Overwriting the source makes debugging impossible — you cannot re-run the filter if the raw numbers are gone.

A filter keeps items that match a rule. A map transforms each item (C to F). You can do both: if t >= 35: hot.append(t * 9 / 5 + 32) — but prefer one idea per loop while learning.

This is what pandas filter is

In pandas you will write hot = temps[temps >= 35]. Under the hood it is still “keep rows where the condition is true”. Learning the loop version means you can read the library version instead of treating it as magic.

Words that matter

Filter
Keep items that match a rule.
Map
Transform each item into a new value.

Common mistakes

Avoid: Deleting from the list you are looping over.

Do this: Build a new list of keepers.

Example program — Heatwave flags (°C)

Threshold 35. Add a 41 and Run.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Your turn — Pass marks

marks = [32, 41, 55, 28]. Collect values >= 40 into passed. Print so 41 and 55 both appear.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Self-assessment

Check your understanding before you mark the lesson complete.

1. Filtering should usually…

Progress is stored in a browser cookie on this device.