Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Module

df.agg

DataFrame.agg applies one or more aggregation operations over a selected axis. It supports single operations, lists of operations, per-column dictionaries, and named aggregations.

How it works

DataFrame.agg applies one or more aggregation operations along a chosen axis. Pass a callable or operation name, a list for multiple operations, or a dictionary to specify operations by column; additional positional and keyword arguments are forwarded to the aggregation function.

Syntax

DataFrame.agg(func=None, axis=0, *args, **kwargs)

Parameters

NameTypeDescription
funcfunction, str, list or dictFunction, string, list, or dictionary specifying the operation or operations used for aggregation.
axis{0 or ‘index’, 1 or ‘columns’}, default 0Axis over which to aggregate: 0 or index for columns, or 1 or columns for rows. Defaults to 0.
*argstupleAdditional positional arguments passed to the aggregation function.
**kwargsdictAdditional keyword arguments passed to the aggregation function.

Returns

scalar, Series or DataFrame, depending on the object and aggregation specification.

Key points

  • Use agg when you need one or several reductions, such as sum, mean, or min, over a DataFrame axis.
  • The func argument identifies the aggregation and may be a callable, operation name, list, or column-to-operation mapping.
  • The default axis=0 reduces each column independently and returns results aligned to the column labels.
  • Use axis=1, or the equivalent columns label, to calculate an aggregation separately for every row.
  • Every aggregation has an explicit direction: across the index for column-wise work or across columns for row-wise work.
  • aggregate is the longer equivalent spelling, so code can use either DataFrame.agg or DataFrame.aggregate.

Common mistakes

  • A callable must accept the DataFrame itself or be usable by DataFrame.apply; functions designed only for individual scalar values may not work as expected.

Example: Aggregate columns with different operations

import pandas as pd

df = pd.DataFrame({"A": [1, 4, 7], "B": [2, 5, 8]})
result = df.agg({"A": ["sum", "min"], "B": "mean"})
print(result)

Computes multiple aggregations for column A and the mean for column B.

When to use it

Column summaries

You need totals, extrema, or averages for every column in one operation.

Per-column aggregation

Different columns require different reductions, such as sum for one and mean for another.

Multiple reductions

You need several statistics for each column, such as sum and minimum.

Row-wise aggregation

You want a row-level statistic across columns.

Interview questions

QWhat does the axis argument control?

By default, agg uses axis=0, so each function is applied to every column. Set axis=1 or axis="columns" to aggregate across each row instead.

Follow-up: What result shape should you expect from several functions? A DataFrame is returned, with aggregation names represented in the result index when aggregating columns.

QWhich forms can func take?

func can be a callable, a string naming an operation, a list of operations, or a dictionary mapping columns to operations. A list requests multiple aggregations, while a dictionary allows different operations for different columns.

Follow-up: How would you combine named results? Use named aggregation keyword arguments such as x=("A", "max") and y=("B", "min").

QWhat can DataFrame.agg return?

For DataFrame.agg, a single aggregation generally produces a Series, while multiple aggregations produce a DataFrame. The documented return possibilities are scalar, Series, or DataFrame depending on the object and aggregation used.

Follow-up: What is the related method name? aggregate is an alias for agg, so either spelling invokes the same operation.

  • DataFrame.aggregate
  • DataFrame.apply
  • Series.agg
  • Series.aggregate

Source: pandas.DataFrame.agg. Checked 2026-10-15.