Data science

Stage 1 · Lesson 5 of 7

Grouping and summarising

Python for data: NumPy and pandas

Grouping splits rows by one or more columns, computes a summary for each group, and combines the results into a new table: people call it split, apply, combine. It is how you get orders per region, average score per or revenue per month.

In it is groupby followed by agg; in R it is group_by followed by summarise. Name your output columns clearly and count rows in each group alongside every average, so a group of 3 is not mistaken for one of 3,000.

Common trapaveraging averages: the mean of per-region means is not the overall mean when regions have different numbers of rows, so compute overall figures from the raw rows or weight by group size.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

  • CoursePaid
    Grokking Python Fundamentals

    Design Gurus · Beginner · $50

    Solid Python basics first: data types, collections, functions and files, the ground pandas stands on

  • CourseFree
    Kaggle Learn

    Kaggle · Beginner

    Free: the Python and Pandas courses, short lessons with exercises that run in the browser

Practise

  • Pick one dataset on a topic you care about and repeat every operation in this stage on it

Keep handy

  • The indexing, merging and reshaping pages answer most "how do I" questions

  • The absolute beginners guide, then the reference when an array does something unexpected

Also worth your time

Mark this stage as not done?