set_categories in pandas: Sort Categorical Column by a Specific Ordering

If you want to sort pandas dataframe’s categorical column by a specific ordering such as small, medium, large, use df.col.cat.set_categories() method.

The code snippet above shows how it works.

Scroll to Top