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 created from a list consisting of five strings and three distinct values: low, medium, and high. When creating the Categorical, pandas determines each unique value in the list and uses those as the categories.
These categories can be examined...