Grouping
Grouping: Grouping with groupby, Several Summaries at Once: agg, Shares and Percentages of a Total.
Updated
- Grouping with groupby (Apprentice): groupby sorts a table's rows into groups that share a value, such as a neighborhood, works out one number for each group (a sum, a mean, a count) and hands back one row per group, which answers "which one sold the most?" in a line instead of a filter for every group.
- Several Summaries at Once: agg (Journeyman): agg works out several summaries for each group in one table, from a list such as ['sum', 'mean', 'count'] or by named aggregation, which names each new column and the column and summary it comes from; groupby takes a list of columns to group by two things at once, and reset_index or as_index=False turns the group labels back into ordinary columns.
- Shares and Percentages of a Total (Journeyman): A group's total means more beside the whole, so divide each group's total by the grand total and multiply by 100 for its share as a percentage; transform('sum') lines each group's total up with every row, so each row's share of its own group is one division; and shares are rounded to one decimal place only at the end, for people to read.