MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Multiple Aggregations & NamedAgg0%
Study MaterialsCheatsheet
PandasProgress 0%
1.1What is Pandas & Core Architecture1.2Pandas Installation & Environment Setup1.3Creating Series & DataFrames from Scratch
6.1GroupBy Mechanics: Split-Apply-Combine6.2Multiple Aggregations & NamedAgg6.3Pivot Tables & Cross-Tabulations (pivot_table, crosstab)
HomeTutorialsPandasMultiple Aggregations & NamedAgg
Pivot Tables & Cross-Tabulations (pivot_table, crosstab)

Multiple Aggregations & NamedAgg

Intermediate6 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. Beyond Single Aggregations

In business reporting, a manager rarely wants just the sum of sales. They want the Sum, Average, Minimum, Maximum, and Transaction Count all in one table.

Pandas provides the .agg() (or .aggregate()) method to compute multiple metrics simultaneously across different columns.


2. Using .agg() with Multiple Built-in Functions

1import pandas as pd
2 
3df = pd.DataFrame({
4 'Branch': ['Delhi', 'Delhi', 'Mumbai', 'Mumbai', 'Delhi'],
5 'Revenue': [120000, 85000, 140000, 190000, 95000],
6 'Expenses': [45000, 30000, 50000, 70000, 35000]
7})
8 
9# Multiple aggregations on a single column
10summary = df.groupby('Branch')['Revenue'].agg(['count', 'sum', 'mean', 'max'])
11print(summary)

Output:

1 count sum mean max
2Branch
3Delhi 3 300000 100000.000000 120000
4Mumbai 2 330000 165000.000000 190000

3. Applying Different Aggregations to Different Columns

Pass a dictionary where keys are column names and values are lists of aggregation functions:

1custom_metrics = df.groupby('Branch').agg({
2 'Revenue': ['sum', 'mean'],
3 'Expenses': ['sum', 'max']
4})
5print(custom_metrics)

4. Modern Best Practice: Named Aggregations (NamedAgg)

When using standard .agg(), Pandas generates hierarchical multi-level column headers (e.g. ('Revenue', 'sum')), which are awkward to query.

Named Aggregation allows you to specify clean, custom output column names directly during the aggregation:

1# Modern clean syntax (Pandas 0.25+)
2clean_report = df.groupby('Branch', as_index=False).agg(
3 Total_Revenue=pd.NamedAgg(column='Revenue', aggfunc='sum'),
4 Avg_Revenue=pd.NamedAgg(column='Revenue', aggfunc='mean'),
5 Max_Expense=pd.NamedAgg(column='Expenses', aggfunc='max'),
6 Transactions=pd.NamedAgg(column='Revenue', aggfunc='count')
7)
8print(clean_report)

Output:

1 Branch Total_Revenue Avg_Revenue Max_Expense Transactions
20 Delhi 300000 100000.0 45000 3
31 Mumbai 330000 165000.0 70000 2

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Which method allows computing multiple statistical aggregations (e.g. sum, mean, max) simultaneously in a GroupBy?
Question 2
What problem does pd.NamedAgg solve in Pandas?
Question 3
How do you apply 'mean' to column 'Age' and 'sum' to column 'Salary' in a single groupby call?
Question 4
Can custom Python functions or lambda expressions be passed into .agg()?
Question 5
What is the difference between agg('count') and agg('size')?

Next Lesson

Pivot Tables & Cross-Tabulations (pivot_table, crosstab)

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
GroupBy Mechanics: Split-Apply-CombinePivot Tables & Cross-Tabulations (pivot_table, crosstab)

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

12-Month Data Analysis Mastery Program

Master Full-Stack Data Analytics (Excel, SQL, Python, Power BI & Tableau)

Combine 15 complete courses, 150 chapters, 541 topics, live SQL/Power BI/Tableau dashboards, and Scikit-Learn capstones at MSK Institute Shikohabad or Live Online.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext