category dtype

noun

say it: “KAT-uh-gor-ee DEE-type” /ˈkætəˌɡɔri ˈdiˌtaɪp/

A pandas column type for values from a short, fixed list, such as shirt sizes, that stores each label once and can keep the labels in a set order.

What is the category dtype in pandas?

The category dtype is a pandas column type for values that come from a short, fixed list, such as T-shirt sizes. Each label is stored once, which saves memory, and the list can keep an order of your own, so S sorts before M before L instead of alphabetically.

import pandas as pd
sizes = pd.Series(["M", "S", "L", "S"])
sizes = sizes.astype(pd.CategoricalDtype(["S", "M", "L"], ordered=True))
print(sizes.sort_values().tolist())   # ['S', 'S', 'M', 'L']

In a sentence

  • “With the category dtype, the sizes sorted S, M, L instead of alphabetically.”
  • “The race's size column became the category dtype, since only five sizes are allowed.”

Level: Freelancer · More Python terms at this level

Updated