v Encoding Ordinal Categorical Features - Machine Learning

Encoding Ordinal Categorical Features

Preliminaries

# Load library
import pandas as pd

Create Feature Matrix

# Create features
df = pd.DataFrame({'Score': ['Low', 
                             'Low', 
                             'Medium', 
                             'Medium', 
                             'High']})

# View data frame
df
Score
0 Low
1 Low
2 Medium
3 Medium
4 High

Create Scale Map

# Create mapper
scale_mapper = {'Low':1, 
                'Medium':2,
                'High':3}

Map Scale To Features

# Map feature values to scale
df['Scale'] = df['Score'].replace(scale_mapper)

# View data frame
df
Score Scale
0 Low 1
1 Low 1
2 Medium 2
3 Medium 2
4 High 3