IconResources
Conduct analysis

Aggregate

Summarize your data by grouping rows and applying calculations like sum or average.

Example Data

Follow along with right out of the box example data. Copy following data in the information request of the agent you are working in.



Aggregation helps us see the big picture. Instead of looking at individual rows, we group rows together to calculate totals, averages, or other statistics. For example, we might want to know the total transaction value per country, or the number of services provided by each seller. These summaries are essential for decision-making and presentation.

In this section, we’ll use Python to group your data and apply common operations like sum(), mean(), and count().

Group by one column and sum

Excel

In Excel, you use pivot tables or functions like SUMIF() to summarize by category.

t0 Prompt

Total amount by seller country

Show me the sum per country

Group by column X and add the values

Code

The python code looks as follows:

transactions["Amount"] = pd.to_numeric(transactions["Amount"])
summary = transactions.groupby("Seller Country")["Amount"].sum()
summary

Group by one column and calculate multiple stats

Excel

In Excel, a pivot table can be used to show multiple statistics at once — like sum, count, and average.

t0 Prompt

Show count, average, and total amount by transaction type

Give me summary stats per transaction category

Code

The python code looks as follows:

transactions["Amount"] = pd.to_numeric(transactions["Amount"])
stats = transactions.groupby("Transaction")["Amount"].agg(["count", "mean", "sum"])
stats

Group by two columns

Excel

In Excel, you might add a second row label to a pivot table to group by two dimensions.

t0 Prompt

Total amount by buyer and seller

Group by country and transaction type

Code

The python code looks as follows:

transactions["Amount"] = pd.to_numeric(transactions["Amount"])
double_group = transactions.groupby(["Seller Country", "Transaction"])["Amount"].sum()
double_group
FunctionDescription
groupby("Column")["Amount"].sum()Total by group
agg(["count", "mean", "sum"])Multiple stats in one step
groupby(["Col1", "Col2"])Group by more than one dimension

On this page