Replacing & Mapping Values

Python · Data Cleaning

Theory

Several methods change values without loops. s.replace('Bombay', 'Mumbai') swaps exact values, and a dict replaces many at once; df.replace({'city': {'Bombay': 'Mumbai'}}) limits it to one column, and regex=True matches patterns. s.map(dict) looks every value up in a dict and returns the mapped value - values missing from the dict become NaN, which is a handy way to spot unmapped codes. map also accepts a function.

For conditional changes, s.where(cond, other) keeps values where the condition is True and replaces the rest, while s.mask(cond, other) does the opposite and replaces where True. s.clip(lower=0, upper=5000) caps values into a range, useful for taming extreme amounts. np.where(cond, a, b) and np.select([conds], [values], default) build new columns from conditions, the pandas way to write CASE WHEN.

Why we use it

Real data uses codes, old names and inconsistent labels ('Bombay' vs 'Mumbai', 'M'/'F', 'Y'/'N'). Replacing and mapping standardises them so grouping and joining work correctly.

When to use it

Use replace for fixing known bad values, map for translating codes through a lookup (with NaN showing what is unmapped), where/mask for conditional overrides and clip for capping. For lookups that need several columns, merge with a lookup table instead.

How it works

  1. Column with values to change
  2. Fixing a few known values? Yes: s.replace({old: new}). No: Translate or override.
  3. Translating codes via a lookup? Yes: s.map(dict); NaN = unmapped. No: Conditional change.
  4. Use where / mask / clip or np.select for conditions
  5. Standardised column

Syntax

s.replace(old, new) / s.replace({old: new})
s.map(dict_or_func)
s.where(cond, other); s.mask(cond, other)
s.clip(lower, upper)
np.select(conditions, choices, default)

Example

df['city'] = df['city'].replace({'Bombay': 'Mumbai'})
df['zone'] = df['city'].map({'Jaipur': 'North', 'Pune': 'West'})

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic