Python DataFrame GroupBy Example: Master Data Aggregation Like a Pro

Logan Jul 13, 2026

Data processing in Python often involves working with DataFrames, a two-dimensional labeled data structure. When dealing with vast datasets, it becomes imperative to summarize and analyze data based on certain criteria. This is where the groupby function comes into play, allowing us to split our DataFrame into groups based on one or more columns, then apply a function to obtain results.

Summary Statistics by Group of pandas DataFrame in Python (Example)
Summary Statistics by Group of pandas DataFrame in Python (Example)

Python's pandas library, which specializes in data manipulation and analysis, provides the groupby functionality. Here, we'll explore a practical example and walk through its intricacies.

Pandas GroupBy: Group, Summarize, and Aggregate Data in Python
Pandas GroupBy: Group, Summarize, and Aggregate Data in Python

Grouping DataFrame by a Single Column

Let's start by grouping a DataFrame using a single column. Assume we have sales data for a clothing store stored in a DataFrame, df. Our goal is to calculate total sales for each category.

Python array sorting guide #python #programming #tutorial
Python array sorting guide #python #programming #tutorial

We'll use the column 'category' to group our data:

```python grouped_df = df.groupby('category') ```

Group Description

Data Analyst project using Python
Data Analyst project using Python

When we call 'grouped_df', we get a new object of type DataFrameGroupBy. This object has several attributes and methods that allow us to manipulate our data in powerful ways:

Let's print the first level of the grouping (the column names):

```python print(grouped_df.grouper.name) ```

Applying Aggregations

Pandas DataFrame - GeeksforGeeks
Pandas DataFrame - GeeksforGeeks

Now, let's calculate the sum of 'sales' for each category:

```python sales_by_category = grouped_df['sales'].sum() print(sales_by_category) ```

Grouping DataFrame by Multiple Columns

More often than not, you'll want to group your DataFrame by multiple columns. Suppose we have another column, 'year', and we want to see total sales by category for each year.

Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)

We can do this by listing multiple columns in the groupby function:

```python grouped_df = df.groupby(['category', 'year']) ```

Group Description

an image of a bar chart with numbers and symbols on it, as well as data
an image of a bar chart with numbers and symbols on it, as well as data
the visualizing data with python
the visualizing data with python
Python 3 and Data Visualization
Python 3 and Data Visualization
Python Learning Path for Beginners 2026 🚀
Python Learning Path for Beginners 2026 🚀
Calculate Sum by Group in Python (2 Examples) | DataFrame Subgroups | groupby() & sum() Functions
Calculate Sum by Group in Python (2 Examples) | DataFrame Subgroups | groupby() & sum() Functions
PySpark Groupby Explained with Example
PySpark Groupby Explained with Example
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
Python For Data Analysis: A Complete Guide For Beginners
Python For Data Analysis: A Complete Guide For Beginners
Python Graph Gallery
Python Graph Gallery
Python data model cheat sheet #tutorial #progrmming #python
Python data model cheat sheet #tutorial #progrmming #python
GitHub - vega/altair: Declarative visualization library for Python
GitHub - vega/altair: Declarative visualization library for Python
the cover of collection data types in python, with an image of numbers and symbols
the cover of collection data types in python, with an image of numbers and symbols
Data Structure in Python #database #javascripts #projects #iot #programminglife #sql #programmerl...
Data Structure in Python #database #javascripts #projects #iot #programminglife #sql #programmerl...
the four basic python data structures are shown in this screenshoter's guide
the four basic python data structures are shown in this screenshoter's guide
Pandas Crosstab Explained
Pandas Crosstab Explained
Feature Engineering with Python Cheat Sheet (Chapter 9/15) | Python for Data Analysts
Feature Engineering with Python Cheat Sheet (Chapter 9/15) | Python for Data Analysts
Data Visualization in Python using Matplotlib
Data Visualization in Python using Matplotlib
Python vs Javascript
Python vs Javascript
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
a diagram that shows how to use the python roadmap for web design and development
a diagram that shows how to use the python roadmap for web design and development

Now, our group keys are tuples containing category and year. Let's print them:

```python print(grouped_df.grouper) ```

Applying Aggregations

Finally, we calculate the sum of 'sales' for each year and category:

```python sales_by_category_year = grouped_df['sales'].sum() print(sales_by_category_year) ```

Remember, this is just the beginning. The groupby function provides many more features, like applying functions to multiple columns, handling missing data, and spline groups. At its core, groupby is a crucial tool for transforming raw data into valuable insights.

Now that you've seen grouping in action, why not try it on your own data? Use the groupby function to extract new, meaningful information from your DataFrames. Happy exploring!