Comparison and Other Common Operations

Discover the comparison and common operations that can be performed on categorical data.


While categorical data may appear less flexible than numerical data in terms of the availability of defined pandas operations, there are still several operations we can apply to them once they are encoded accordingly with dtype='category'. In particular, let’s explore the comparison and common operations that can be implemented on categorical data.


Recall that pandas comes with a set of logical comparison operations for us to compare values in a DataFrame. In the case of categorical data, there are two scenarios where we can apply comparison operations on them. We’ll reuse the credit card dataset to demonstrate these two scenarios.

Unordered categories

When we have a set of unordered categories, we can compare their equality with a list-like object (e.g., Series, list, or NumPy array) of the same length as the categorical data. For example, we can compare the first five values of the Gender categorical column with a Series object, comprising five Male values, as shown below:

Get hands-on with 1200+ tech skills courses.