liveBook · Manning

Transform

As explained in The Well-Grounded Data Analyst

The pandas transform method applies a function to grouped data and returns results aligned with the original data shape.

Use pandas `transform` when a grouped calculation must remain beside the rows that produced it. It supports window-function-like analysis without creating a separate summary table and joining it back. section 9.2.1

Hourly share of daily cycling

After aggregating cycling counts to one row per location, date, and hour, group by location and date and transform `Pedal_cycles` with `sum`. The resulting daily total is repeated on each hourly row, so dividing the hourly count by that total yields `hourly_pct`. Check a location's raw records to confirm both the total and percentages before using them. section 9.2.1

cycling_daily_hourly['TotalDailyCount'] = (
    cycling_daily_hourly
    .groupby(['Count_point_id', 'Count_date'])
    ['Pedal_cycles']
    .transform('sum')     #1
)

cycling_daily_hourly['hourly_pct'] = (
    cycling_daily_hourly['Pedal_cycles']
    / cycling_daily_hourly['TotalDailyCount']
)

cycling_daily_hourly

This listing appends each group's daily total, then calculates the hourly proportion from values aligned to the hourly rows. section 9.2.1

How The Well-Grounded Data Analyst covers it

How deep it goes
Demonstrates grouped `sum` transformation in a worked pandas calculation and verifies its output against raw records.
Examples
Uses latest-date cycling counts by location, date, and hour, including a raw-data check for location 900056.
What it assumes
Assumes pandas grouping and aggregation while contrasting the approach with SQL window functions.
What you can do after
Apply it via pandas to add grouped totals and derive row-level percentages without a join.
How it connects
Connects pandas grouping to SQL window functions, aggregation, filtering, and percentage calculations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Data Analyst. The section links point to the parts of the book it draws on.