liveBook · Manning

Total row

As explained in Build Python Web Apps with Streamlit

A total row is a table row containing aggregate values for all records or groups represented in the table.

A total row summarizes every record or group in a drilldown table. It helps readers understand the whole before comparing individual dimension combinations. Dimension cells are intentionally blank except for the label “Total.” section 7.5.3 section 7.5.2

Build the row in a few deliberate steps:

  • Calculate total metrics from the original granular dataframe, because metric calculations expect raw, non-aggregated data rather than the already grouped table. section 7.5.3
  • Create blank values for every selected dimension and place “Total” in the first dimension column. section 7.5.3
  • Prepend the total row to the aggregated dataframe with pandas.concat so it appears above the detailed rows. section 7.5.3
  • When no dimensions are selected, create a single-row dataframe with an empty dimension column, the “Total” label, and the calculated metrics. section 7.5.3
  • Format metric values and shade the row so users can distinguish the summary from the breakdown. section 7.5.3

A drilldown summary

Two product rows show $100,000 and $200,000 in sales. A total row above them shows $300,000, leaves the dimension cells blank, and labels the first dimension “Total.” section 7.5.3

+--------+----------------+--------------+-------------+--------------+----------+-------+
| Gender |    Segment     | Product name | Total sales | Gross margin | Margin % | ATV   |
+--------+----------------+--------------+-------------+--------------+----------+-------+
| Total  |                |              |   $300,000  |  $210,000    |   70%    | $20   |
|   M    | Fountain pens  | InkStream    |   $100,000  |   $60,000    |   60%    | $10   |
|   M    | Fountain pens  | RoyalQuill   |   $200,000  |  $150,000    |   75%    | $40   |
+--------+----------------+--------------+-------------+--------------+----------+-------+

Use the completed table shape to inspect how the total appears above detailed rows and how aggregate values relate to them. section 7.5.3

How Build Python Web Apps with Streamlit covers it

How deep it goes
Explains construction, raw-data dependencies, no-dimension handling, insertion order, formatting, and styling.
Examples
Uses product-level sales and margin rows followed by a labeled, blank-dimension total row.
What it assumes
Assumes familiarity with dataframe aggregation and metric functions while teaching the total-row assembly from its component operations.
What you can do after
Build it from scratch by calculating raw-data metrics, constructing the summary row, prepending it, formatting values, and styling its display.
How it connects
Connects drilldown tables with data aggregation, Pandas dataframes, metric calculation, and Streamlit table presentation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.