Repair the count to distinct customers
Change one line so the code counts customers instead of orders, then check the new number a second way.
2 parts · 4 and 6 min
Free course. Your Maestro, an AI, .
Before you touch a line of code, pause on the result from the last page: what did the AI's 6 actually count? When you printed the filtered table and wrote down the customer for each row by hand, you listed six paid orders placed by only four customers: C01, C03, C04, and C06. Two of those buyers placed a second paid order, and each extra purchase added another row to the table.
The AI's Glossary word code ran without an error and calculated a real number. It answered "How many paid orders are in the table?" because the Glossary word of shop-orders is one row per order. Running len(paid) counts the rows that survived the filter, so it counts paid orders.
The question you asked had a different grain: "How many customers have at least one paid order?" To answer a question about customers on a table of orders, the count must move from rows to distinct values in the customer_id column. Calling .nunique() on a column counts each distinct value once, even when that value appears on multiple rows. The question's grain decides which function you call: here the question asks about customers on an orders table, so .nunique() on customer_id fits the question, whereas if a teammate asked how many paid orders shipped, len(paid) would be the right calculation.
Why two common fixes still give 6
When a row count does not match the question, two quick edits often come to mind first. On this table, both still print 6:
- Counting distinct customers on the whole table. Running
data["customer_id"].nunique()counts distinct customer IDs across all eight rows ofdatainstead of the six rows ofpaid. That returns 6 because it includes C02 and C05, two customers who only have"cancelled"orders. Getting 6 again is easy to miss because it matches the AI's 6, even though the AI counted six paid orders and this line counts six total customers. - Dropping duplicate rows before counting. Running
len(paid.drop_duplicates())checks whether any two rows match across every column (order_id,customer_id,status, andamount). Inshop-orders, no row is a duplicate. Every row is a separate order with a uniqueorder_id, so.drop_duplicates()removes nothing andlen(paid.drop_duplicates())still prints 6. If you want to trace why both of those edits return 6 on these eight rows, ask your Maestro to walk through the table with you.
Repairing the count to match the question's grain
Worked example · 3 steps
You want to answer: "How many customers have at least one paid order?" The orders table is loaded as data, where one row is one order (order_id), customer_id identifies the buyer, and status is either "paid" or "cancelled". Walk through the three moves that produce the customer count.
Work out the next step yourself, then open it to compare.
A good place to stop. Up next: Check the new number a second way, then report both counts · 6 min
Check the new number a second way, then report both counts
Changing len(paid) to paid["customer_id"].nunique() gives you 4. Before you pass that number on, check it a second way by listing who those four customers are. Calling paid["customer_id"].unique() pulls the distinct customer IDs from paid, and wrapping it in sorted(...) orders them alphabetically: ['C01', 'C03', 'C04', 'C06']. When that list has four items and matches your hand count from the last page item for item, both your count and the list of buyers agree.
When you report the repaired number to someone who already saw the AI's 6, put both numbers and what they count in one honest sentence:
"4 customers had at least one paid order (6 paid orders in all)."
That single sentence answers the question about customers and explains at the same time where the 6 came from.
Try it
In the exercise below, the AI's filter and row count are already in place. Uncomment and complete the two lines at the bottom so the code prints the number of distinct customers in paid and then prints the sorted list of their customer_id values.
You are done when the output shows AI count (paid rows): 6, followed by Customers with a paid order: 4 and ['C01', 'C03', 'C04', 'C06'], matching the four customers from your hand count on the last page. If your count still prints 6 or you hit an error, open your Maestro and share what ran.