liveBook · Manning

Standard

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

Value standardization is the transformation of inconsistent categorical labels into a consistent set of labels.

Value standardization turns inconsistent categorical labels into a consistent vocabulary. First inspect distinct and frequent values; then build a translation table and replace the original values. section 5.14

Standardizing vehicle colors

A parking-ticket dataset contains 1,896 recorded color labels, including abbreviations and misspellings. A dictionary can map variants such as WH, BK, GRY, and WHT to WHITE, BLACK, and GRAY. Applying the mapping creates a new Series that can replace the original column. Unmapped values remain unchanged, and matching is exact, including case, punctuation, and whitespace; recounting distinct values helps reveal missed or incorrect mappings. section 5.14

len(df['Vehicle Color'].value_counts().index)

Count distinct labels before and after replacement to measure whether standardization reduced inconsistency. section 5.14

df['Vehicle Color'].value_counts().head(30)

Inspect the most common labels before designing the mapping. section 5.14

colormap = {'WH': 'WHITE', 'GY':'GRAY', 'BK':'BLACK',
           'BL':'BLUE', 'RD':'RED', 'SILVE':'SILVER',
           'GR':'GRAY', 'TN':'TAN', 'BR':'BROWN',
           'YW':'YELLO', 'BLK':'BLACK', 'GRY':'GRAY',
           'WHT':'WHITE', 'WHI':'WHITE', 'OR':'ORANGE',
           'BK.':'BLACK', 'WT':'WHITE', 'WT.':'WHITE'}

Represent known variants and their desired canonical labels in a Python dictionary. section 5.14

df['Vehicle Color'] = df['Vehicle Color'].replace(colormap)

Apply the dictionary with Series.replace and assign the returned Series back to the column. section 5.14

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Works through inspection, mapping, replacement, and recounting, while noting exact matching and unmapped values.
Examples
Uses parking-ticket vehicle colors, abbreviations, misspellings, a colormap dictionary, and before-and-after distinct-value counts.
What it assumes
Assumes basic pandas Series and column assignment, while demonstrating dictionaries and value_counts in the cleaning task.
What you can do after
Apply it via pandas to standardize a categorical Series and diagnose missed mappings by recounting distinct labels.
How it connects
Connects value_counts, pandas Series.replace, dictionaries, and data-cleaning pipelines.

Also explained in

Compare how 6 books explain standard →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.