Average bill per visit, or per line?
Diagnose why an AI's average visit bill is far too low, then compute the average at the visit grain.
2 parts · 5 and 5 min
Free course. Your Maestro, an AI, .
A clinic manager asked: "What is our average bill per visit?" Every visit at the clinic starts with an $80 consultation, yet the AI answered $74.29 after running lines["charge"].mean() on the billing table below.
visit_id | item | charge |
|---|---|---|
| V1 | consult | 80 |
| V1 | x-ray | 120 |
| V1 | lab | 40 |
| V2 | consult | 80 |
| V3 | consult | 80 |
| V3 | lab | 40 |
| V4 | consult | 80 |
How can the average visit bill sit below $80 when every single visit includes an $80 consult and some visits add an x-ray or a lab test on top?
Diagnose the divisor before you repair the code
Before you edit a calculation in Glossary word, write down the fraction the AI actually computed. Every Glossary word has a numerator (what got added up) and a denominator (how many items it was divided by).
Adding the seven numbers in the charge column gives $80 + $120 + $40 + $80 + $80 + $40 + $80 = $520. That $520 is the exact revenue billed across all four visits. The AI's numerator is right.
The trouble is in the denominator. One row in lines has the Glossary word of a single billed item within a visit. When the AI ran lines["charge"].mean(), it divided $520 by 7 billing lines and got $74.29 per line.
To answer the manager's question, the denominator needs to be the 4 distinct visits: V1, V2, V3, and V4. Look at what happens on visit V3 when the clinician orders a $40 lab test after the $80 consult:
- V3's visit bill goes up from $80 to $120.
- In the AI's row-level average, that extra $40 row increases the row count by 1 and pulls the average charge per line down.
If it seems strange that adding a $40 lab test raises a patient's bill while pulling the AI's average down, ask your Maestro to walk through visit V3.
Watching the denominator also steers you away from three tempting dead ends:
- Assuming the same total gives the same average. Sharing the $520 total only guarantees the same numerator. Dividing $520 by 7 lines ($74.29) and dividing $520 by 4 visits ($130.00) only agree when every visit has exactly one line.
- Dropping the cheaper lines. Filtering out the $40 lab rows leaves 5 rows totaling $440, which averages $88.00 per remaining line and throws away $80 of real revenue.
- Taking the Glossary word of the column. Running
lines["charge"].median()returns $80.00, the middle item charge, which still describes a single line rather than a whole visit bill.
Compute the average at the visit grain
Worked example · 3 steps
The clinic table lines has 7 item rows totaling $520 across 4 visits (V1, V2, V3, V4). Repair the AI's lines["charge"].mean() in three steps so it computes the average bill per visit.
Work out the next step yourself, then open it to compare.
A good place to stop. Up next: part 2 of 2 · 5 min
Try it
Now run the clinic calculation yourself. First run the AI's line-level mean to see 74.29. Then group lines by visit_id, sum charge to see each visit's total bill, and compute the per-visit average in two ways that must agree: taking .mean() on the visit totals, and dividing lines["charge"].sum() by lines["visit_id"].nunique().
When your code runs, check the three parts of the output:
- The grouped visit totals show V1 at 240, V2 at 80, V3 at 120, and V4 at 80.
- Both
per_visit.mean()andlines["charge"].sum() / lines["visit_id"].nunique()print 130.0. - In your reply to the clinic manager, you can state both the diagnosis and the corrected figure: the AI divided the $520 in charges by 7 billing lines ($74.29 per line), whereas dividing across the 4 visits gives an average bill of $130.00 per visit.
If your per-visit average and your divisor check do not print the same number, share your output with your Maestro.