Summarising and reshaping

Sorting and ranking, value counts, grouping with aggregation and transform, and reshaping with pivot_table, melt and crosstab.

About 6 hours of study

Learning objectives

Lessons

  1. 1

    Sorting, ranking and counting Intermediate

    Ordering rows with sort_values and sort_index, sorting by several columns in different directions, where missing values go, ranking with ties, the top rows with nlargest, and counting values with value_counts.

  2. 2

    groupby: split, apply, combine Intermediate

    Grouping rows by one or more keys and summarising each group with sum, mean, count, size and agg, the difference between count and size, transform for per-row results, and filter for keeping whole groups.

  3. 3

    Reshaping: pivot_table, melt and crosstab Advanced

    Turning long data into a wide grid with pivot and pivot_table (and why duplicates need an aggregation), turning wide data back into long rows with melt, and counting combinations with crosstab, with margins for totals.

Module practice

Module quiz

1. What does s.value_counts() return?
2. df.groupby('city')['sales'].sum() gives…
3. melt turns…
4. How does groupby().transform('mean') differ from agg('mean')?
5. rank(method='min') gives tied values…
6. pivot raises an error but pivot_table works. Why?

‹ Selecting, cleaning and transformingCombining tables, text and dates ›