Named Aggregation

noun

say it: “NAYMD ag-ruh-GAY-shun” /ˈneɪmd ˌæɡrəˈɡeɪʃən/

A way of asking pandas for group summaries where you name each new column and say which column and summary it comes from.

What is named aggregation in pandas?

Named aggregation is a way of writing agg() where each summary is new_name=(column, summary). Each result becomes a column with the name you chose, worked out from the column you chose, so the table reads plainly.

import pandas as pd
orders = pd.DataFrame({"size": ["small", "large", "small"], "total": [12.0, 20.0, 14.0]})
summary = orders.groupby("size").agg(orders=("total", "count"), sales=("total", "sum"))
print(summary)
#        orders  sales
# size
# large       1   20.0
# small       2   26.0

In a sentence

  • “With named aggregation, the columns came out as orders and sales instead of count and sum.”
  • “She used named aggregation so the report's headers needed no renaming.”

Level: Freelancer · More Python terms at this level

Updated