ZetCode

Pandas Aggregation

last modified November 17, 2026 Python 3.15

An aggregation reduces many values to one summary value, such as a total, an average or a maximum. Pandas provides the agg method, and its full-name alias aggregate, for this job. This article is a direct follow-up to the Pandas grouping data tutorial: there we split data into groups, and here we summarise the results.

The method works on a single Series or on a whole DataFrame. You can name the functions to run, give each column its own set of functions, or attach new names to the results. When it is combined with groupby, one summary value is produced per group.

A single function on a Series

On a Series, agg with one function returns a single number, a scalar. The function can be passed as a string holding its name; quoted names like 'sum' and 'mean' refer to built-in Pandas operations. Writing aggregate instead of agg does exactly the same thing.

agg_series_one.py
import pandas as pd

s = pd.Series([10, 20, 30, 40])
print(s.agg('sum'))

The string 'sum' tells Pandas to add the four values together, so the result is 100. A real Python function, such as the built-in max, can be passed without quotes as well; both forms are accepted.

$ python agg_series_one.py
100

Several functions on a Series

Passing a list of function names runs every function and returns a Series whose index contains the function names and whose values are the results. This is a compact way to see several summaries of the same column at once.

agg_series_many.py
import pandas as pd

s = pd.Series([10, 20, 30, 40])
print(s.agg(['sum', 'mean', 'max']))

The output has three entries: the total 100, the average 25 and the largest value 40. They are labelled with the names we supplied. The average makes the result a floating-point Series, which is why the integers are shown with a decimal point.

$ python agg_series_many.py
sum     100.0
mean     25.0
max      40.0
dtype: float64

Different functions for each column

On a DataFrame, a dictionary maps each column name to the function that should be applied to it. The result is a Series with one entry per column that was mentioned. Columns left out of the dictionary are not aggregated.

agg_columns.py
import pandas as pd

df = pd.DataFrame({'price': [10, 20, 30], 'quantity': [1, 2, 3]})
print(df.agg({'price': 'sum', 'quantity': 'mean'}))

Here the prices are added up to 60 while the quantities are averaged to 2. The dictionary keys price and quantity become the index of the result, so it is easy to tell which summary belongs to which column.

$ python agg_columns.py
price       60.0
quantity     2.0
dtype: float64

Several functions per column

The value in that dictionary can itself be a list. Pandas then computes every listed function for that column and returns a table: the rows are labelled by function names and the columns by the original column names. A column that did not request a particular function shows NaN in that row.

agg_many_per_column.py
import pandas as pd

df = pd.DataFrame({'price': [10, 20, 30], 'quantity': [1, 2, 3]})
print(df.agg({'price': ['sum', 'mean'], 'quantity': ['min', 'max']}))

The table has four rows. The sum and mean rows hold values only under price, while min and max hold values only under quantity. The empty cells are filled with NaN because no function was requested for that combination.

$ python agg_many_per_column.py
      price  quantity
sum    60.0       NaN
mean   20.0       NaN
min     NaN       1.0
max     NaN       3.0

Named aggregations

Keyword arguments give a name to each result: name=(column, function). This replaces the automatic function names with names that describe the result, such as total instead of sum. It also fixes the column mentioned in each calculation, which makes the intent clear.

agg_named.py
import pandas as pd

df = pd.DataFrame({'price': [10, 20, 30], 'quantity': [1, 2, 3]})
result = df.agg(total=('price', 'sum'), average=('price', 'mean'), smallest=('price', 'min'))
print(result)

The three keywords become the labels total, average and smallest, each calculated from the price column. On the pandas 3.1.1 version used to verify these examples, the result is a table with those labels as rows and price as its only column; earlier pandas versions may return the same information as a simple Series. Either way the numbers are the ones requested.

$ python agg_named.py
          price
total      60.0
average    20.0
smallest   10.0

Aggregating grouped data

The real power of agg shows up after groupby. When the DataFrame is split into groups, each named aggregation produces one value per group. The result is a table indexed by the group labels, with one column for each named summary.

agg_groupby.py
import pandas as pd

df = pd.DataFrame({
    'category': ['A', 'B', 'A', 'B'],
    'price': [10, 20, 30, 40],
    'quantity': [1, 2, 3, 4]
})
print(df.groupby('category').agg(total=('price', 'sum'), count=('price', 'count')))

There are two categories. For A the prices 10 and 30 add up to 40, and for B the prices 20 and 40 add up to 60; each group contains two rows. The keywords total and count become the column names of the result. See the Pandas grouping data tutorial for more on how groups are formed.

$ python agg_groupby.py
          total  count
category              
A            40      2
B            60      2

Source

Pandas DataFrame.aggregate Documentation, Pandas Series.aggregate Documentation, and Pandas Group By Documentation

In this article we have aggregated Series and DataFrames with agg, used function names as strings, requested several summaries at once, named the results with keyword arguments, and combined everything with groupby. For applying a function without reducing the data see Pandas apply, map and transform, and for repeating group summaries back onto the rows see the grouping data tutorial.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Pandas tutorials.