Data Science

Label Encoding for Ordinal Variables

Senior Data Analyst
May 3, 2026
5 min read

Label Encoding

from sklearn.preprocessing import LabelEncoder

le = LabelEncoder()

df['size_encoded'] = le.fit_transform(df['size']) # S=0, M=1, L=2

Manual Mapping (More Control)

size_map = {'Small': 1, 'Medium': 2, 'Large': 3}

df['size_num'] = df['size'].map(size_map)

*Day 124: Scaling and Normalization.*

Ready to put your knowledge into practice?

Join SQL Mastery and learn through interactive exercises.