Finding the best syntax for Aggregation in Python

And the winner is….

Anmol Tomar


AI-Generated Image by Author


GroupBy operation is one of the most used data manipulation operations in data analysis — allowing us to split the dataset into groups based on some columns and then perform operations such as mathematical aggregations on these groups.

In Pandas, there are multiple syntaxes available for using GroupBy, each offering its own advantages and convenience. In this blog, we’ll explore and compare the different syntaxes for GroupBy in Pandas, providing examples and insights into when to use each.

1. Method Chaining Syntax

Method chaining syntax is a powerful technique used in pandas for performing multiple operations on a DataFrame concisely and efficiently. Instead of applying each operation separately and assigning intermediate results to variables, method chaining allows us to chain together multiple methods in a single expression.

With method chaining, each method call is applied sequentially to the DataFrame, with the output of one method serving as the input to the next.

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({
'Category': ['A', 'B', 'A', 'B', 'A'],
'Profit': [10, 20, 30, 40, 50],
'Sales': [90, 180, 150…



Anmol Tomar
Writer for

Top AI writer | Data Science Manager | Mentor. Want to kick off your career in Data Science? Get in touch with me: