Skip to content
Start free

How do you recompute an AI's number with pandas?

Read five lines of pandas, run them on the hardware store's file, and check the two numbers the AI gave.

Free course. Your Maestro, an AI, .

On this page you recompute the two numbers from the example answer ("312 sales" and "average ticket of $378.83") with a calculator of your own. The calculator is , a Python tool for working with tables. You won't learn to program: you will learn to read and change five kinds of lines.

In every exercise in this course, the file is already loaded into a table called data. It is synthetic data, CC0: the hardware store and its sales are made up, so you can use and share them however you like. The columns keep their Spanish names: ticket, fecha (date), sucursal (branch), producto (product), cantidad (quantity), importe (amount), and estado (status, either pagado for paid or cancelado for canceled). First, a warm-up with a three-row table. Before you run it, write down what you think it will print.

A tiny table: two tickets, and the first one has two products. Write what you think each program prints, and why, before you run it.

Counting rows and summing a column

Example 1 of 2

example.py
import pandas as pdtabla = pd.DataFrame({    "ticket": ["T-1", "T-1", "T-2"],    "importe": [100, 50, 300],})print(len(tabla))print(tabla["importe"].sum())

A guess is fine. What you write stays in this browser and waits in your course.

Example 2 of 2, opens once you explain this one

Five lines you will see everywhere

These are the lines that come up most when an AI calculates something with pandas. If you recognize them, you can read most of what it shows you.

Python
len(data)                              # how many rows the table hasdata["importe"].sum()                  # sum of the importe columndata["importe"].mean()                 # average of the importe columndata["sucursal"] == "Zapopan"          # for each row: is it from Zapopan? (yes or no)data[data["sucursal"] == "Zapopan"]    # a new table, with only the Zapopan rows
Whatever comes after # is a comment: Python ignores it, and it is there for you.

How to read them:

  • data is the whole table. data["importe"] is a single column of that table, the one called importe. The brackets and quotation marks are how you say "this column."
  • .sum(), .mean() and len(...) are the operations: add up, average, count rows. There are more, but these three take you far.
  • == asks "is it equal to?" The fourth line changes nothing: it answers yes or no for each row.
  • The fifth line uses that answer to keep only the rows that said yes. It is a filter, like filtering a column in Excel. Whatever you filter there changes every number you calculate afterward, which is why filters are the first thing you look for in an AI's code.

If a line confuses you, don't memorize it: ask. Explain the filter line again with an Excel example.

Checking "312 sales" and "$378.83"

Worked example · 4 steps

The AI answered "312 sales, with an average ticket of $378.83." You want to know where those two numbers come from in the file. Try each step before opening it.

Work out the next step yourself, then open it to compare.

Try it

Now it's your turn with the whole file. The code is already written: run it with Run (or Ctrl + Enter, ⌘ + Enter on a Mac) and compare each number with the AI's answer. Then add the lines for Zapopan. If you get an error, read the line it points to: it is almost always a missing quotation mark or parenthesis. I got an error and I don't know what it means.

Your turn

Run the code and compare it with the AI's answer (312 sales, $378.83). Then, below the last line, filter the Zapopan rows into a table called zapopan and print how many rows it has and how much its importe adds up to. You are done when the output says Zapopan has 89 rows, the number from the previous page.

Data: ventas-marzo, 312 rows
The exercise's data, "ventas-marzo". Your edits run with your code.
RowRemove
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Page 1 of 16

If your numbers match the AI's, good: you have a reproducible calculation consistent with its result. That is half of the check. The other half is asking whether what it did answers what you needed, and that is what the next modules are for.

A note on how the output looks: pandas writes 118196.0 with a dot and a zero at the end because it stores the amounts as numbers with decimals. It is the same as 118,196. And round(promedio, 2) only rounds to make it easier to read; it doesn't change the calculation.

Check yourself: You ran len(data) and got 312, the same as the AI's "312 sales." What do you know now?