Collapse spelling and case variants to a single canonical label. A variant→canonical dict maps every known form (lowercased) to the standard value. By hand, lowercase each label and look it up in the dict. With pandas, chain .str.lower() with .map(dict) to do the same in one expression.

By hand

With pandas

.str.lower() lowercases the column in one pass, then .map(dict) remaps each element to its canonical value. All inputs have a dict entry, so no NaN appears in the result.

naive.py
variant_to_canonical = {
    'usa': 'US', 'u.s.a.': 'US', 'united states': 'US',
    'uk': 'UK',
}
labels = ['USA', 'usa', 'U.S.A.', 'United States', 'uk', 'UK']
result = []
for s in labels:
    result.append(variant_to_canonical[s.lower()])
print('RESULT:', result)
library.py
import pandas as pd
from dalib.display import set_display
set_display()

variant_to_canonical = {
    'usa': 'US', 'u.s.a.': 'US', 'united states': 'US',
    'uk': 'UK',
}
labels = ['USA', 'usa', 'U.S.A.', 'United States', 'uk', 'UK']
df = pd.DataFrame({'c': labels})
s = df['c'].str.lower().map(variant_to_canonical)
result = s.tolist()
print('index:', s.index.tolist())
print('dtype:', s.dtype)
print('RESULT:', result)
index: [0, 1, 2, 3, 4, 5]
dtype: object
RESULT: ['US', 'US', 'US', 'US', 'UK', 'UK']

Implementation notes

  • Lowercase before the lookup so the dict only needs one entry per variant, not one per case combination.
  • If an input has no dict entry, .map(dict) returns NaN for that element rather than raising — add a .fillna() or verify coverage before mapping.
  • Cross-reference: map-values (python-pandas ch08) for the general elementwise remap pattern this lesson applies to canonicalization.