Creating Categoricals

A pandas Categorical is used to represent a categorical variable. A categorical variable consists of a finite set of values and is often used to map values into a set of categories and track how many values are present in each category. Another purpose is to map sections of continuous values into a discrete set of named labels, an example of which is mapping a numeric grade to a letter grade. We will examine how to perform this mapping at the end of the chapter.

There are several ways to create a pandas Categorical. The following screenshot demonstrates creating a Categorical directly from a list:

This Categorical is ...

Get Learning pandas - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.