liveBook · Manning

Data grouping

As explained in Build Python Web Apps with Streamlit

Pandas groupby is a dataframe operation that partitions rows into groups and supports aggregation across those groups.

Data grouping partitions dataframe rows by a shared value so each group can be processed separately. In a dashboard, filtering may leave many product, gender, and state rows for each day, while the chart needs one total per day. Pandas groupby performs that reduction across the selected dimensions. section 6.5.1 section 6.6

Aggregate daily sales

With a dataframe named df, group rows by Day, sum Sales within each group, and restore Day as a regular column: grouped = df.groupby('Day') data = grouped.apply( lambda df: df['Sales'].sum(), include_groups=False ).reset_index() For two CA rows on 2024-08-01—1500 and 1300—the result contains one row for that date with Sales equal to 2800. The grouped object represents the rows belonging to each day. apply runs the function separately for every group; include_groups=False prevents the function from operating on the group labels, and reset_index converts the resulting Day index back into a column. section 6.5.1

A reusable helper can apply a configured metric and rename the result columns to Day and Value. That grouped result can then feed a Plotly line chart displayed in Streamlit, allowing the same dashboard pattern to show several metrics.

How Build Python Web Apps with Streamlit covers it

How deep it goes
Explains grouping, per-group apply, group labels, and index restoration, then stops short of broader aggregation patterns.
Examples
Uses filtered California sales rows, daily totals, a reusable metric helper, and a Plotly time-series chart.
What it assumes
Assumes familiarity with pandas dataframes, columns, filtering, and basic Python functions.
What you can do after
Apply via pandas to produce grouped daily data for dashboard visualizations.
How it connects
Connects pandas dataframes, filtering, aggregation, time series, Plotly, and Streamlit.

Also explained in

Compare how 6 books explain data grouping →

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.