GroupBy—The groupby() method
Understand and apply the basic capabilities of the groupby() method.
Overview of groupby()
The groupby()
method is a powerful and versatile method that allows us to group rows or columns together based on the criteria we define. It’s often used with aggregate functions (e.g., sum, average, etc.) or transformation functions (e.g., normalization) to summarize results across one or more columns.
The groupby()
method is commonly used in real-world data analysis because it helps us create useful high-level data summaries. For example, a bank may want to determine the customers’ average account balances over the last three months. A groupby()
operation with a mean aggregation will give us the answer needed.
A groupby()
operation combines the following three steps to group large datasets together and perform computations on them:
Split data into groups based on the criteria we specify.
Apply a function to each group independently.
Combine results in a data structure.
In the retail bank example above, we first split the data based on records for each unique customer and compute the average bank balance for each customer over the three-month period. Then, we return the output as a DataFrame where each row represents a unique customer. The code for the above example—groupby()
with mean()
—is shown below:
# Groupby customer ID and calculate average (mean)df_grouped = df[['Customer ID', 'Balance']].groupby(by='Customer ID').mean()print(df_grouped)
Note: The groups created from the
groupby()
method are calledGroupBy
objects.
The groupby()
parameters
Let's take a closer look at the parameters of the groupby()
method to understand its extensive ... ...
Get hands-on with 1400+ tech skills courses.