🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.agg()

AI & DATA SCIENCE // df-agg

df.agg() applies one or more aggregation functions to grouped (or ungrouped) data, letting you compute several different summary statistics — even different ones per column — in a single call.

Syntax

df.agg(func)

Deep Dive Course

Passing a single function name applies it uniformly; passing a list computes multiple aggregations at once, each becoming its own row or column in the result; and passing a dict applies a specific, different aggregation to each named column. It's most commonly chained after groupby() to compute several distinct per-group statistics in one pass, rather than calling separate methods like .sum() and .mean() and combining their results manually afterward.

1Understanding df.agg()

Passing a single function name applies it uniformly; passing a list computes multiple aggregations at once, each becoming its own row or column in the result; and passing a dict applies a specific, different aggregation to each named column. It's most commonly chained after groupby() to compute several distinct per-group statistics in one pass, rather than calling separate methods like .sum() and .mean() and combining their results manually afterward.

💡

Pass a dict to agg(), mapping each column name to its own list or name of aggregation functions, to compute different, specifically-tailored aggregations for different columns in one call, instead of chaining multiple separate aggregation calls and manually combining their results.

editor.html
import pandas as pd

df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
print(df.groupby("team")["score"].agg(["sum", "mean"]))
localhost:3000

2Practical Example

Here is a real-world application of df.agg() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30], "age": [22, 25, 30]})
print(df.groupby("team").agg({"score": "sum", "age": "max"}))
localhost:3000

3Best Practices

Follow these guidelines when working with df.agg():

1. Use agg() with a dict to compute different, appropriately-chosen aggregations per column in one pass, rather than several separate calls

2. Chain agg() after groupby() for multi-statistic per-group summaries, instead of computing each statistic with a separate method call

3. Pass a list of functions to a single column's agg() call when you need multiple different summaries of that one column at once

⚠️

Tip: Pass a dict to agg(), mapping each column name to its own list or name of aggregation functions, to compute different, specifically-tailored aggregations for different columns in one call, instead of chaining multiple separate aggregation calls and manually combining their results.

editor.html
import pandas as pd

df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
print(df.groupby("team")["score"].agg(["sum", "mean"]))
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
print(df.groupby("team")["score"].agg(["sum", "mean"]))
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30], "age": [22, 25, 30]})
print(df.groupby("team").agg({"score": "sum", "age": "max"}))

Best Practices

  • Use agg() with a dict to compute different, appropriately-chosen aggregations per column in one pass, rather than several separate calls
  • Chain agg() after groupby() for multi-statistic per-group summaries, instead of computing each statistic with a separate method call
  • Pass a list of functions to a single column's agg() call when you need multiple different summaries of that one column at once

Interview Question

How would you compute both the sum and the mean of a column for each group in a single call, rather than calling groupby().sum() and groupby().mean() separately?

Hint: Think about what argument type agg() accepts for multiple aggregations at once.

Chain .agg() onto the groupby() result, passing a list of the aggregation function names you want, like a list containing 'sum' and 'mean' — agg() computes every function in that list for each group in a single pass over the data, returning a result with one column per requested aggregation. This is both more concise and more efficient than calling separate aggregation methods and then manually joining their results together afterward, since it only needs to iterate over the grouped data once.

Exercises

MediumPractice using df.agg() in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
print(df.groupby("team")["score"].agg(["sum", "mean"]))

Frequently Asked Questions

How would you compute both the sum and the mean of a column for each group in a single call, rather than calling groupby().sum() and groupby().mean() separately?

Chain .agg() onto the groupby() result, passing a list of the aggregation function names you want, like a list containing 'sum' and 'mean' — agg() computes every function in that list for each group in a single pass over the data, returning a result with one column per requested aggregation. This is both more concise and more efficient than calling separate aggregation methods and then manually joining their results together afterward, since it only needs to iterate over the grouped data once.

Related Functions

df-groupbydf-transformdf-sum