Working with Categorical Data in Python
Learn to store, clean, reorder, plot and encode categorical columns in Python with the pandas category type, seaborn charts and one-hot encoding.
Columns that hold labels such as blood type or marital status turn up in nearly every dataset, and they carry common pitfalls when treated like ordinary text. You learn to store these columns with the pandas category type, keep their categories tidy and in a useful order, summarize them with grouped statistics, chart them with seaborn, and turn them into numeric codes that machine learning algorithms can read.
What You Will Achieve
- Convert label columns to the category dtype when reading a file or creating a Series, and check the memory it saves
- Clean messy label values and filter a DataFrame on its categories without breaking the column’s type
- Compare numerical summaries across groups with seaborn box, bar, point and count charts
- Prepare categorical variables for machine learning with label or one-hot codes and reuse a saved mapping
Program Overview
Categorical Data Types and Grouping in pandas
Sets out what makes a variable categorical and how pandas stores and summarizes it.
- Categorical vs. numerical and ordinal variables
- The category dtype and memory savings
- Setting dtypes while reading data
- .groupby() summaries by category
Managing Categories in a pandas Series
Works on the categories themselves, then applies the changes to clean and access data in a DataFrame.
- Setting, adding and removing categories
- Updating, renaming and collapsing categories
- Reordering categories before grouping
- Cleaning variables and filtering data
Seaborn Plots, Pitfalls and Encoding
Charts categorical columns with seaborn, then turns to common mistakes and the encodings used before modeling.
- Box, bar, point and count plots with catplot()
- Hue, category order and one plot per group
- String issues and NumPy array pitfalls
- Label, Boolean and one-hot encoding
Certificate Awarded (Paid)
Job Outlook
Handling categorical columns well is part of everyday data analysis and data science work, where survey, census and review data are summarized, charted and encoded before they reach machine learning models.
Good for: Python users who already work with DataFrames and keep meeting label columns that need cleaning, ordering, charting or encoding.
Keep in mind: Exercises run on pandas and seaborn, and the encoding lessons, label and one-hot, stop at preparing columns for machine learning algorithms.
Bottom line: A tight focus on one pandas data type, with enough seaborn charting and encoding practice to carry categorical columns through an analysis.
Frequently Asked Questions
What kinds of data are the exercises built on?
The practice data includes characteristics of adoptable dogs, reviews of trips to Las Vegas and census records.
Why store a column as the category type?
The opening chapter shows that keeping labels in the category dtype can save memory, and how to set that type when reading data or building a Series so later grouping and reordering rely on it.
What pitfalls with categorical columns are covered?
The final chapter looks at common mistakes, including string issues and the use of NumPy arrays with categorical values, and includes a knowledge check on memory usage before moving on to encoding.


There are no reviews yet.