agg()

noun

say it: “AG” /ˈæɡ/

Short for aggregate: a pandas method that works out several summaries for each group, such as a total, an average and a count, in one table.

What does agg() do in pandas?

agg(), short for aggregate, is a pandas method that works out several summaries for each group at once, such as a sum, a mean and a count, and hands them back as one table with a column for each.

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

In a sentence

  • “agg() gave each pizza size its total, average and number of orders.”
  • “One call to agg() replaced three separate groupby lines.”

Level: Journeyman · More Python terms at this level

Updated