Topic Overview

Hello and welcome! In this lesson, we are going to learn how to convert categorical data into ordered types using the Diamonds dataset from the seaborn library. The goal of this lesson is to enable you to transform categorical data into ordered categorical types effectively. Understanding this process is crucial for improving data analysis and visualization.

Introduction to Categorical Data

Categorical data is data that can be divided into groups or categories. For example, the grades students receive (A, B, C, etc.), types of cars (SUV, Sedan, Truck), and the levels of satisfaction in a survey (Poor, Fair, Good, Very Good, Excellent) are all examples of categorical data.

In the Diamonds dataset, we have categorical columns such as cut, color, and clarity:

  • cut describes the quality of the diamond cut (e.g., Fair, Good, Very Good, Premium, Ideal).
  • color indicates the color grading of a diamond (e.g., D, E, F, G, H, I, J).
  • clarity represents the clarity of the diamond (e.g., I1, SI2, SI1, VS2, VS1, VVS2, VVS1, IF).
Understanding Categorical Data Conversion

Converting categorical data to ordered types is essential for several reasons:

  • Sorting: Ordered categorical data can be sorted meaningfully.
  • Analysis: Many statistical analyses and visualizations require data to be ordered.
  • Representation: Ordered types provide a clear hierarchy or ranking for categorical variables.

For example, in the context of diamond quality:

  • Cut: Fair < Good < Very Good < Premium < Ideal
  • Color: J < I < H < G < F < E < D
  • Clarity: I1 < SI2 < SI1 < VS2 < VS1 < VVS2 < VVS1 < IF
Converting Categorical Data to Ordered Types

To convert the categorical columns in our dataset to ordered types, follow these steps:

  1. Define the category order: First, specify the order of the categories for cut, color, and clarity.

    cut_categories = ['Fair', 'Good', 'Very Good', 'Premium', 'Ideal']
    color_categories = ['J', 'I', 'H', 'G', 'F', 'E', 'D']
    clarity_categories = ['I1', 'SI2', 'SI1', 'VS2', 'VS1', 'VVS2', 'VVS1', 'IF']
  2. Convert to categorical types: Use the pd.Categorical method from Pandas to specify the order for each categorical column.

    diamonds['cut'] = pd.Categorical(diamonds['cut'], categories=cut_categories, ordered=True)
    diamonds['color'] = pd.Categorical(diamonds['color'], categories=color_categories, ordered=True)
    diamonds['clarity'] = pd.Categorical(diamonds['clarity'], categories=clarity_categories, ordered=True)
  3. Verify the conversion: Print the cat.ordered attribute to confirm that the columns have been converted correctly. You can also confirm the order of the categories by accessing categories, as shown in the code below.

    # Confirm the conversions
    print(diamonds['cut'].cat.ordered)
    print(diamonds['color'].cat.ordered)
    print(diamonds['cut'].cat.ordered)
    
    # Print the order
    print(diamonds['cut'].cat.categories)

The output of the above code will be:

True
True
True
Index(['Fair', 'Good', 'Very Good', 'Premium', 'Ideal'], dtype='object')

This output shows the data types of each column after conversion, indicating that cut, color, and clarity have been successfully converted to ordered categorical types, which will allow for more meaningful sorting and analysis.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal