When we group data, we are aggregating the data by category. This can be very useful especially when we need to get a high-level view of a detailed dataset. Typically, to group a dataset, we need to identify the column/category to group by, the column to aggregate by, and the specific aggregation to be done. The column/category to group by is usually a categorical column while the column to aggregate by is usually a numeric column. The aggregation to be done can be a count, sum, minimum, maximum, and so on. We can also perform aggregation such as count directly on the categorical column we group by
In pandas, the groupby method helps us group data.
Getting ready
We will work with one dataset in this chapter – the Marketing Campaign data from Kaggle.
Create a folder for this chapter and create a new Python script or Jupyter notebook file in that folder. Create a data subfolder and place the marketing_campaign.csv file in that subfolder. Alternatively, you can retrieve all the files from the GitHub repository.
Note
Kaggle provides the Marketing Campaign data for public use at https://www.kaggle.com/datasets/imakash3011/customer-personality-analysis. In this chapter, we use both the full dataset and samples of the dataset for the different recipes. The data is also available in the repository. The data in Kaggle appears in a single-column format, but the data in the repository was transformed into a multiple-column format for easy usage in pandas.
How to do it…
We will learn how to group data using the pandas library:
- Import the
pandas library:import pandas as pd
- Load the
.csv file into a dataframe using read_csv. Then, subset the dataframe to include only relevant columns:marketing_data = pd.read_csv("data/marketing_campaign.csv")marketing_data = marketing_data[['ID','Year_Birth', 'Education','Marital_Status','Income','Kidhome', 'Teenhome', 'Dt_Customer', 'Recency','NumStorePurchases', 'NumWebVisitsMonth']]
- Inspect the data. Check the first few rows and use
transpose (T) to show more information. Also, check the data types as well as the number of columns and rows:marketing_data.head(2).T
0 1
ID 5524 2174
Year_Birth 1957 1954
Education Graduation Graduation
… … …
NumWebVisitsMonth 7 5
marketing_data.dtypes
ID int64
Year_Birth int64
Education object
… …
NumWebVisitsMonth int64
marketing_data.shape
(2240, 11)
- Use the
groupby method in pandas to get the average number of store purchases of customers based on the number of kids at home:marketing_data.groupby('Kidhome')['NumStorePurchases'].mean()Kidhome
0 7.2173240525908735
1 3.863181312569522
2 3.4375
That’s all. Now, we have grouped our dataset.
How it works...
All of the recipes in this chapter use the pandas library for data transformation and manipulation. We refer to pandas as pd in step 1. In step 2, we use read_csv to load the .csv file into a pandas dataframe and call it marketing_data. We also subset the dataframe to include only 11 relevant columns. In step 3, we inspect the dataset using the head method to see the first two rows in the dataset; we also use transform (T) along with head to transform the rows into columns, due to the size of the data (i.e., it has many columns). We use the dtypes attribute of the dataframe to show the data types of all columns. Numeric data has int and float data types while character data has the object data type. We inspect the number of rows and columns using shape, which returns a tuple that displays the number of rows as the first element and the number of columns as the second element.
In step 4, we apply the groupby method to get the average number of store purchases of customers based on the number of kids at home. Using the groupby method, we group by Kidhome, then we aggregate by NumStorePurchases, and finally, we use the mean method as the specific aggregation to be performed on NumStorePurchases.
There’s more...
Using the groupby method in pandas, we can group by multiple columns. Typically, these columns only need to be presented in a Python list to achieve this. Also, beyond the mean, several other aggregation methods can be applied, such as max, min, and median. In addition, the agg method can be used for aggregation; typically, we will need to provide specific numpy functions to be used. Custom functions for aggregation can be applied through the apply or transform method in pandas.
See also
Here is an insightful article by Dataquest on the groupby method in pandas: https://www.dataquest.io/blog/grouping-data-a-step-by-step-tutorial-to-groupby-in-pandas/.