Session Ready
Exercise

Category

Pandas provides the category data type, which is analogous to the R factor.

You can convert a column into a categorical data type by passing 'category' to the .astype() method. Once you have a categorical column, you can see the various categories (known as levels in R) by using the .cat accessor and calling the .categories attribute.

Another use case for categorical values is when you want to preserve ordering in your data. For example, intuitively it makes sense that 'low' comes before 'high'. You can use reorder_categories() to provide an order to a column.

# Reorder categorical levels
df['column_name'].cat.reorder_categories(['low', 'high'], ordered=True)
Instructions 1/2
undefined XP
  • 1
  • 2

Convert the type of 'time' column into category and print the categories in this column.