Back to the lesson

Python and AI lab

See the logic of two Python scripts before you run them. Clean a messy dataset and summarize it, then step through a local agent that answers questions by calling Python functions. This page runs in your browser and sends none of your choices anywhere. Like every page on this site, it counts page views with the Foundation's self-hosted metrics, which use no cookies. The scripts in the scripts folder do the same work for real.

1. Clean a messy dataset

Made-up library loans, saved as loans.csv. It has stray spaces, inconsistent capitals, numbers with commas, blanks and duplicates. Switch cleaning steps off to see what goes wrong.

2. Summarize it

Group by
Measure
Only this branch
Top rows: 5
GroupValue

What to notice

  • Cleaning is a list of small, named steps. Each one is a few lines of Python.
  • Skipping a step does not always crash. Often it quietly gives wrong numbers, such as one branch counted as four.
  • The raw file is never changed. The script writes a new loans_clean.csv.
  • The agent in section 3 only sees the cleaned data, through narrow functions.

The Python behind it

This code updates as you change the controls above. The same logic is in wrangle.py and wrangle_pandas.py.

Cleaning (standard library)

Summary (standard library)

Summary (pandas)

3. Step through a local agent

An agent is a model, some tools, and a loop. Pick a question and click Next. The tool results are computed from the cleaned data. The model's replies are scripted for teaching: real models vary.

Question

4. Run the scripts yourself

Open a terminal in the scripts folder. You need Python 3.9 or newer.

This is a teaching tool. It mirrors the scripts but does not run Python. Dedicated to the public domain under CC0 1.0.