Create Catagorical data types in Pandas

  • Create a categorical data type within a series.
s = pd.Series(["a","b","c","a", dtype = "category")
  • By converting an existing series or column to a category dtype.
df = pd.DataFrame({"A":["a", "b", "c", "a"]})

df["B"] = df["A"].astype('category')
  • Create a specific Categorical data type
raw_cat = pd.Categorical(["a", "b", "c", "a"], categories =["b", "c", "d"], ordered=False)

s = pd.Series(raw_cat)

For more information go to: This pandas link