top of page

25% Discount For All Pricing Plans "welcome"

Mastering Categorical Data with Python and Pandas


In the vast world of data science and analysis, a robust understanding of categorical data is a key stepping stone. This comprehensive tutorial aims to guide you through the intricacies of understanding, handling, and analyzing categorical data using Python and the versatile Pandas library.


I. Understanding Categorical Data


A. Definition and Importance of Categorical Data


Categorical data is a type of data that is used to categorize or label entities based on their attributes. Unlike numerical data which is quantitative, categorical data is qualitative, and while numerical data can be measured, categorical data can only be observed.


In the context of a survey, for example, age would be a numerical variable, while the response to a multiple-choice question (such as 'What is your favorite color?') would be a categorical variable.


B. Types of Categorical Data: Ordinal vs. Nominal Variables


Categorical data can further be broken down into two types: Ordinal and Nominal.

  • Nominal data is the simplest form of categorical data, which is used for labelling variables without any order of precedence. For instance, hair color (Black, Brown, Blonde) is a nominal categorical variable.

  • Ordinal data is a type of categorical data with a set order or scale to it. For example, customer satisfaction can be measured on a scale like: "Unsatisfied", "Neutral", "Satisfied". Here, the data is categorized and also has a certain order.


C. Practical Example: Exploring a Dataset with Categorical Data


Let's understand this with the help of Python and a real-world dataset: the adult census income dataset. We will first load the data and then explore the categorical variables in it. The following Python script does this:

import pandas as pd

# Load dataset
data = pd.read_csv('adult.csv')

# Check data types
print(data.dtypes)

# Explore a specific column
print(data['marital-status'].describe())

# Count of each category in marital status
print(data['marital-status'].value_counts())

# Percentage of each category in marital status
print(data['marital-status'].value_counts(normalize=True))


Note: In the output, you'll notice that marital-status is an object type (a string in Pandas), which we can interpret as a categorical variable. 'describe' gives us a high-level overview of the column, while 'value_counts' provides the count and percentage of each category.


II. Working with Categorical Data in Python with

Pandas


A. Introduction to Pandas for Handling Categorical Data


Pandas is a Python library that provides extensive means for data analysis. Data scientists often work with data stored in table formats like .csv, .tsv, or .xlsx. Pandas makes it very convenient to load, process, and analyze such tabular data using SQL-like queries. In conjunction with Matplotlib and Seaborn, Pandas provides a wide range of opportunities for visual analysis of tabular data.

The main data structures in Pandas are implemented with Series and DataFrame classes. The former is a one-dimensional indexed array of some fixed data type. The latter is a two-dimensional data structure - a table - where each column contains data of the same type. You can see it as a dictionary of Series instances.


Remember: Dealing with categorical data is a significant part of data preprocessing (data cleaning) which is the first step in any data science project.


B. Understanding Data Types in Pandas: 'object' vs. 'categorical'


In Pandas, string variables are stored as 'object' data types. However, it is more memory-efficient and faster to store categorical data as 'categorical' data types, especially when the number of categories is less compared to the number of observations.


C. Creating a Categorical Series in Pandas


A categorical series can be created in two ways:

  1. Using pd.Series

s = pd.Series(["a","b","c","a"], dtype="category")
print(s)

  1. Using pd.Categorical

s = pd.Categorical(["a", "b", "c", "a"])
print(s)


Both of these codes will give you the same output: a categorical series.


D. Importance of Storing Data as 'categorical' dtype in Pandas


Let's take an example to demonstrate the importance of using 'categorical' data type. If we have a series with a million observations of a variable that takes on only five distinct categories, storing it as 'object' dtype would consume significantly more memory compared to 'categorical' dtype.


E. Specifying Data Types when Reading Data in Pandas


When loading data, you can specify the data type of a column directly using an argument in the read_csv() function:

data = pd.read_csv('adult.csv', dtype={'marital-status': 'category'})


In this case, the marital-status column is read directly as a 'categorical' dtype, thereby optimizing memory usage.


III. Grouping Categorical Data in Pandas


A. Introduction to Grouping Data by Category in Pandas


Often in data analysis, we need to group our data by certain characteristics,

perform calculations on these groups, and then compare the groups to each other. This process is called "grouping". Pandas provides a flexible and efficient groupby() operation that allows us to slice, dice, and summarize datasets in a manner that is natural to humans.

Imagine you're in a fruit shop with a bag of different fruits. Now, if I ask you to group these fruits by their type, you'll end up with groups of apples, bananas, oranges, etc. Each group is a category and that's exactly what we're going to do with our data.


B. Understanding the Basics of '.groupby()' Function in Pandas


The Pandas groupby() function is quite versatile and allows us to group data in various ways, providing a powerful tool for data analysis. Here's a simple analogy: imagine we're working with a dataset of various books, and we want to find out which author has the highest average book rating. We could use groupby() to group all books by the author, then calculate the average rating for each author's books.


In code, this might look like:

# Assuming 'df' is our DataFrame and it has columns 'author' and 'rating'
average_ratings = df.groupby('author')['rating'].mean()
print(average_ratings)


In the output, you'll see each author's name along with their average book rating.

The groupby() function works in a few stages:

  1. Splitting: The data is divided into separate groups based on some criteria (in our example, the 'author' column).

  2. Applying: A function is applied to each group independently (we applied 'mean' to the 'rating' column).

  3. Combining: The results of the function applications are combined into a new data structure (our output is a new Series with authors and their average ratings).


C. Specifying Columns When Grouping


You can specify the columns to be included in the grouping. For example, let's say you want to group by 'author' and only include the 'rating' and 'price' columns. Here's how you can do it:

grouped = df.groupby('author')[['rating', 'price']]
print(grouped.mean())

In the output, you'll see each author with their average rating and average price.


D. Grouping by Multiple Columns


You can also group by multiple columns. For example, if you want to group by both 'author' and 'publisher', you can do it like this:

grouped = df.groupby(['author', 'publisher'])['rating'].mean()
print(grouped)


This will show the average rating for each author and publisher combination.

To check the size of each group (i.e., how many entries belong to each group), you can use the size() function:

grouped_size = df.groupby(['author', 'publisher']).size()
print(grouped_size)


This will output a Series with multi-indexes (author, publisher) and the size of each group.


Note: It's crucial to understand that Pandas allows flexibility in manipulating data and applying a broad range of methods after the grouping operation.


IV. Visualizing and Analyzing Categorical Data


Now that we have explored the different ways to process and analyze categorical data, it's time to bring in another powerful tool: data visualization. Visualizations help us understand our data more intuitively and identify patterns that might not be immediately apparent from looking at raw data.


A. Introduction to Data Visualization with Matplotlib and

Seaborn


In Python, Matplotlib and Seaborn are two of the most widely used libraries for creating static, animated, and interactive visualizations. Seaborn is actually built on top of Matplotlib and provides a high-level interface for drawing attractive and informative statistical graphics.


To continue our analogy from before, visualizing data is like giving a tour of a zoo. The data is like different species of animals, and the visualization is like the map that shows where each species is located, how many of them there are, and what characteristics they have.

Firstly, we need to import the necessary libraries:

import matplotlib.pyplot as plt
import seaborn as sns


B. Bar Plots for Categorical Data


Bar plots are great for visualizing the distribution of categorical data. For example, let's visualize the distribution of 'marital-status' from our adult census dataset:

plt.figure(figsize=(10, 6))  # setting the figure size
sns.countplot(data=df, x='marital-status')  # creating the plot
plt.title('Distribution of Marital Status')  # adding a title
plt.xticks(rotation=45)  # rotating the x-axis labels for better readability
plt.show()  # displaying the plot

In the output, you'll see a bar plot showing the number of occurrences of each category in the 'marital-status' column.


C. Box Plots for Categorical vs Numerical Data


Box plots are excellent for visualizing the relationship between a categorical feature and a numerical feature. They provide a summary of the statistical properties (minimum, first quartile, median, third quartile, maximum) of the numerical feature for each category of the categorical feature.

For example, if we want to understand the relationship between 'marital-status' and 'age', we could use a box plot:

plt.figure(figsize=(12, 8))
sns.boxplot(data=df, x='marital-status', y='age')
plt.title('Age Distribution by Marital Status')
plt.xticks(rotation=45)
plt.show()


The resulting plot displays a box for each marital status category, where the box represents the interquartile range (from Q1 to Q3), the line inside the box is the median, and the 'whiskers' represent the range of the data (excluding potential outliers, which are represented as individual points).

Understanding the structure and visualization of data is crucial in data analysis and machine learning tasks. It helps to reveal hidden patterns, correlations, outliers, or trends in the data that can lead to more accurate models and predictions.


By now, you've gained valuable insights into handling, processing, and visualizing categorical data in Python. The skills and tools discussed throughout this tutorial are fundamental for any data science enthusiast or professional. With practice and creativity, you can harness these techniques to unveil the power of data in your projects and tasks. Happy data journey!

Commentaires


bottom of page