Using .map() and .replace()
Welcome to **Day 119**. Today we learn how to "Translate" categories.
The Mapping Pattern
Instead of a big `CASE` statement (SQL) or many `IF`s (Python), we use a dictionary.
status_map = {
'S': 'Shipped',
'P': 'Pending',
'C': 'Cancelled'
}
df['status_full'] = df['status_code'].map(status_map)
.replace()
`.replace()` is similar but works for specific values across the whole DataFrame.
# Replace multiple errors at once
df = df.replace({'Error': 0, 'Incomplete': -1})
Why .map() is faster
Behind the scenes, `.map()` is highly optimized. It's the standard way to turn "Status Codes" into "Human Readable Labels" in a production pipeline.
Your Task for Today
Create a mapping dictionary for 'US States' (e.g., {'NY': 'New York'}) and apply it to a column using `.map()`.
*Day 120: Ranking and Binning Data (pd.cut).*