Recipe: How to encode ordinal categorical features in Python?
DATA MUNGING CATEGORICAL VARIABLE EXAMPLES

How to encode ordinal categorical features in Python?

This recipe helps you encode ordinal categorical features in Python
In [1]:
## How to encode ordinal categorical features in Python 
def Kickstarter_Example_31():
    print()
    print(format('How to encode ordinal categorical features in Python', '*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # Load library
    import pandas as pd

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

    # View data frame
    print(); print(df)

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

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

    # View data frame
    print(); print(df)

Kickstarter_Example_31()
***************How to encode ordinal categorical features in Python***************

    Score
0     Low
1     Low
2  Medium
3  Medium
4    High

    Score  Scale
0     Low      1
1     Low      1
2  Medium      2
3  Medium      2
4    High      3


Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe
1 developer from HvH
1 developer from MSI Services
1 developer from Altimetrik
1 developer from Infosys
1 developer from MudraCircle
1 developer from ANAC
1 developer from KPMG
1 developer from Vodafone
1 developer from Ekimetrics
1 developer from LTI