Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Exploratory Data Analysis with Python Cookbook

You're reading from   Exploratory Data Analysis with Python Cookbook Over 50 recipes to analyze, visualize, and extract insights from structured and unstructured data

Arrow left icon
Product type Paperback
Published in Jun 2023
Publisher Packt
ISBN-13 9781803231105
Length 382 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Ayodele Oluleye Ayodele Oluleye
Author Profile Icon Ayodele Oluleye
Ayodele Oluleye
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Chapter 1: Generating Summary Statistics 2. Chapter 2: Preparing Data for EDA FREE CHAPTER 3. Chapter 3: Visualizing Data in Python 4. Chapter 4: Performing Univariate Analysis in Python 5. Chapter 5: Performing Bivariate Analysis in Python 6. Chapter 6: Performing Multivariate Analysis in Python 7. Chapter 7: Analyzing Time Series Data in Python 8. Chapter 8: Analysing Text Data in Python 9. Chapter 9: Dealing with Outliers and Missing Values 10. Chapter 10: Performing Automated Exploratory Data Analysis in Python 11. Index 12. Other Books You May Enjoy

Categorizing data

When we refer to categorizing data, we are specifically referring to binning, bucketing, or cutting a dataset. Binning involves grouping the numeric values in a dataset into smaller intervals called bins or buckets. When we bin numerical values, each bin becomes a categorical value. Bins are very useful because they can provide us with insights that may have been difficult to spot if we had worked directly with individual numerical values. Bins don’t always have equal intervals; the creation of bins is dependent on our understanding of a dataset.

Binning can also be used to address outliers or reduce the effect of observation errors. Outliers are unusually high or unusually low data points that are far from other data points in our dataset. They typically lead to anomalies in the output of our analysis. Binning can reduce this effect by placing the range of numerical values including the outliers into specific buckets, thereby making the values categorical. A common example of this is when we convert age values into age groups. Outlier ages such as 0 or 150 can fall into a less than age 18 bin and greater than age 80 bin respectively.

In pandas, the cut method can be used to bin a dataset.

Getting ready

We will work with the full Marketing Campaign data for this recipe.

How to do it…

We will categorize data using the pandas library:

  1. Import the pandas library:
    import pandas as pd
  2. 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']]
  3. 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)
  4. Categorize the number of store purchases into high, moderate, and low categories:
    marketing_data['bins'] = pd.cut(x=marketing_data['NumStorePurchases'], bins=[0,4,8,13],labels = ['Low', 'Moderate', 'High'])
  5. Inspect the result. Subset for relevant columns:
    marketing_data[['NumStorePurchases','bins']].head()
        NumStorePurchases    bins
    0    4    Low
    1    2    Low
    2    10    High
    3    4    Low
    4    6    Moderate

We have now categorized our dataset into bins.

How it works...

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 head(2) to see the first two rows in the dataset; we also use transpose (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 and columns respectively.

In step 4, we categorize the number of store purchases into three categories, namely High, Moderate, and Low. Using the cut method, we cut NumStorePurchases into these three bins and supply the logic for binning within the bin parameter, which is the second parameter. The third parameter is the label parameter. Whenever we manually supply the bin edges in a list as done previously, the bins are typically the number of label categories + 1.

Our bins can be interpreted as 0–4 (low), 5–8 (moderate), and 9–13 (high). In step 5, we subset for relevant columns and inspect the result of our binning.

There’s more...

For the bin argument, we can also supply the number of bins we require instead of supplying the bin edges manually. This means in the previous steps, we could have supplied the value 3 to the bin parameter and the cut method would have categorized our data into three equally spaced bins. When the value 3 is supplied, the cut method focuses on the equal spacing of the bins, even though the number of records in the bins may be different.

If we are also interested in the distribution of our bins and not just equally spaced bins or user-defined bins, the qcut method in pandas can be used. The qcut method ensures the distribution of data in the bins is equal. It ensures all bins have (roughly) the same number of observations, even though the bin range may vary.

You have been reading a chapter from
Exploratory Data Analysis with Python Cookbook
Published in: Jun 2023
Publisher: Packt
ISBN-13: 9781803231105
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime