🚀 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.groupby()

AI & DATA SCIENCE // df-groupby

df.groupby() splits a DataFrame into groups based on the values in one or more columns, enabling per-group aggregation, transformation, or filtering — pandas' equivalent of a SQL GROUP BY.

Syntax

df.groupby(by, as_index=True)

Deep Dive Course

groupby() itself doesn't compute anything immediately — it returns a lazy GroupBy object representing the split-apply-combine plan, which only actually does work once you chain an aggregation, like .sum(), .mean(), or .agg(), or another operation onto it. Grouping by multiple columns, passing a list, creates one group per unique combination of those columns' values, and by default the grouped columns become the resulting index unless you pass as_index=False to keep them as regular columns instead.

1Understanding df.groupby()

groupby() itself doesn't compute anything immediately — it returns a lazy GroupBy object representing the split-apply-combine plan, which only actually does work once you chain an aggregation, like .sum(), .mean(), or .agg(), or another operation onto it. Grouping by multiple columns, passing a list, creates one group per unique combination of those columns' values, and by default the grouped columns become the resulting index unless you pass as_index=False to keep them as regular columns instead.

💡

groupby() alone doesn't print anything useful and doesn't compute results — it returns a lazy GroupBy object that only actually does work once you chain an aggregation method, like .sum() or .mean(), onto it.

editor.html
import pandas as pd

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

2Practical Example

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

editor.html
import pandas as pd

df = pd.DataFrame({"team": ["A", "B", "A", "B"], "score": [10, 20, 15, 25]})
print(df.groupby("team", as_index=False)["score"].mean())
localhost:3000

3Best Practices

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

1. Chain a specific aggregation (.sum(), .mean(), .agg(), etc.) onto groupby() rather than trying to inspect or print the raw GroupBy object directly

2. Pass as_index=False when you want the grouping column(s) to remain regular columns in the result, rather than becoming the new index

3. Group by a list of multiple columns when you need one group per unique combination of several categorical fields, not just one

⚠️

Tip: groupby() alone doesn't print anything useful and doesn't compute results — it returns a lazy GroupBy object that only actually does work once you chain an aggregation method, like .sum() or .mean(), onto it.

editor.html
import pandas as pd

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

Examples

Example 01Basic Usage
import pandas as pd

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

df = pd.DataFrame({"team": ["A", "B", "A", "B"], "score": [10, 20, 15, 25]})
print(df.groupby("team", as_index=False)["score"].mean())

Best Practices

  • Chain a specific aggregation (.sum(), .mean(), .agg(), etc.) onto groupby() rather than trying to inspect or print the raw GroupBy object directly
  • Pass as_index=False when you want the grouping column(s) to remain regular columns in the result, rather than becoming the new index
  • Group by a list of multiple columns when you need one group per unique combination of several categorical fields, not just one

Interview Question

Why does printing the result of df.groupby('team') directly show something unhelpful like a memory address, instead of any actual grouped data?

Hint: Think about what groupby() actually returns before any aggregation is applied.

groupby() itself doesn't compute any grouped results — it returns a lazy GroupBy object that just represents the plan to split the DataFrame into groups, without actually doing any of the work yet. The real computation, splitting the data, applying some operation to each group, and combining the results back together, only happens once you call an actual aggregation or transformation method on that object, like .sum() or .mean(). Printing the bare GroupBy object shows its internal representation rather than any grouped data, since no aggregation has been requested yet.

Exercises

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

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

Frequently Asked Questions

Why does printing the result of df.groupby('team') directly show something unhelpful like a memory address, instead of any actual grouped data?

groupby() itself doesn't compute any grouped results — it returns a lazy GroupBy object that just represents the plan to split the DataFrame into groups, without actually doing any of the work yet. The real computation, splitting the data, applying some operation to each group, and combining the results back together, only happens once you call an actual aggregation or transformation method on that object, like .sum() or .mean(). Printing the bare GroupBy object shows its internal representation rather than any grouped data, since no aggregation has been requested yet.

Related Functions

df-aggdf-transformdf-apply