Skip to content
Start free

Is counting rows the same as counting sales?

Find out what a row of the file represents and count what the question asks for: tickets, not product lines.

Free course. Your Maestro, an AI, .

The first of the three questions is the simplest and the one that saves the most numbers: what does a row represent? In the hardware store's file, a row is not a sale. It is a product within a ticket. If someone walks out with a hammer, nails, and gloves, that ticket takes up three rows.

This is called the grain of a table: the level of detail of each row. The same information can come with one row per ticket, one per product within a ticket, or one per day and branch. None is better than another. What matters is that your count matches what your question is counting.

Warm up with a five-row table. Before you run it, write down what you think it prints:

Three tickets: the first has three products and the other two have one each.

Three tickets: the first has three products and the other two have one each.

Rows versus tickets

Example 1 of 2

example.py
import pandas as pd

tabla = pd.DataFrame({
    "ticket": ["T-1", "T-1", "T-1", "T-2", "T-3"],
    "producto": ["Martillo", "Clavos", "Guantes", "Pintura", "Guantes"],
})

print(len(tabla))
print(tabla["ticket"].nunique())

A guess is fine: it's kept as you wrote it, so you can compare. Only you and your Maestro see your predictions.

Example 2 of 2, opens once you explain this one

Make your prediction

How to tell what a row is

Almost no file says it out loud. You find out with three questions that take a minute:

  1. Which column should identify each thing? In sales, the ticket number. In payroll, the employee ID. In a clinic, the patient record number.
  2. Does that column repeat? If len(data) and data["ticket"].nunique() give the same number, each ticket takes one row. If there are more rows, some tickets have several rows.
  3. What tells two rows of the same ticket apart? Look at two rows with the same ticket number. If the only thing that changes is the product, a row is a product within a ticket. That sentence is the answer you are looking for.

To look at the first rows, the line is data.head(). To compare the two counts:

Python
data.head(8)                  # the first 8 rows, to look at them
len(data)                     # how many rows
data["ticket"].nunique()      # how many distinct tickets
If the two counts don't match, a row is not a ticket.

How many sales were there in Zapopan?

Worked example · 4 steps

The AI answered 89. You want to know whether 89 is a number of sales. Try each step before opening it.

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

Try it

The exercise already counts rows and tickets for the whole month. Run it and compare: 312 rows against 123 tickets. Then do the same for Chapalita, the branch that serves a lot of contractors. Before you run it, guess: will the gap between rows and tickets be big or small in Chapalita? I don't know why the gap is so big in Chapalita.

Run it and compare the first two lines of the output. Then count Chapalita's rows and distinct tickets. You are done when you see Chapalita's two numbers and can say in your own words why they are so far apart.

Run it and compare the first two lines of the output. Then count Chapalita's rows and distinct tickets. You are done when you see Chapalita's two numbers and can say in your own words why they are so far apart.

Run your code to see what it prints and the value of its last expression. Every run starts fresh.

Ready to run your code.

Run your code to see its output here.

Data: ventas-marzo, 312 rows

Enter saves a cell and Escape leaves it as it was. Leave a cell empty for a missing value.

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

Run it yourself

In Chapalita you should see 88 rows and 18 tickets: almost five products per ticket. If an AI told you "Chapalita had 88 sales," it would be counting lines, and Chapalita would look almost tied with Zapopan, the branch with the most rows, when it has 18 tickets to Zapopan's 30 and is one of the branches with the fewest tickets.

Now look at Tlaquepaque: 22 rows and 22 tickets. There, counting rows gives the same result as counting tickets, because every Tlaquepaque ticket has a single product. That coincidence is dangerous: if you check a Tlaquepaque number, everything adds up, and the error only shows up when someone looks at another branch.

Check yourself: In Tlaquepaque, counting rows and counting tickets give the same number: 22. What does that tell you?

Check yourself: Your payroll file has one row per employee per pay period, and people are paid twice a month. You are asked: "How many people worked in March?" What do you count?