Master Pandas From Scratch

Learn Pandas fundamentals, Series, DataFrames, data selection, filtering, cleaning, sorting, grouping, merging, reshaping, string operations, time series, data analysis, performance optimization, visualization, and practical projects through structured lessons.

COURSE SYLLABUS
1. Pandas Introduction and Environment Setup
Introduction to Pandas Installation and Environment Setup
2. Pandas Core Data Structures
Data Structures Overview Pandas Series DataFrames Indexes, Rows and Columns Shape, Size and Dimensions
3. Pandas Data Input and Output
Data Input and Output Overview Reading CSV, Excel and JSON Files Writing and Exporting Files
4. Initial Data Inspection
Data Inspection Overview Head and Tail Data Info and Types Descriptive Statistics
5. Selection and Indexing
Selection and Indexing Overview Column Selection loc and iloc Index Management
6. Filtering and Conditional Selection
Filtering Overview Boolean Filtering Multiple Conditions isin, between and str.contains
7. Basic Data Cleaning
Data Cleaning Overview Missing Values Duplicate Data Rename and Replace Data Type Conversion
8. Vectorized Operations and Sorting
Operations and Sorting Overview Column Operations Sorting Values and Indexes Sum, Mean, Max and Min
9. GroupBy and Aggregation
GroupBy Overview Grouping Data Aggregation Functions Group Filtering Group Transform
10. Merging, Joining and Concatenating
Merge, Join and Concat Overview Concatenation Merging DataFrames Joining by Index
11. Reshaping and Pivoting
Reshaping Overview Pivot Tables Melting Data Stack and Unstack Cross Tabulation
12. String and Categorical Data
String and Categorical Overview String Operations Categorical Data One-Hot Encoding
13. Time Series Analysis
Time Series Overview DateTime Operations Time-Based Indexes Resampling Data Shift and Rolling Windows
14. Custom Functions and Mapping
Apply and Mapping Overview Apply and Lambda Functions Map Operations Element-Wise Operations
15. Performance and Large Datasets
Performance Overview Memory Optimization Processing Files in Chunks PyArrow, Dask and Polars
16. Pandas Data Visualization
Visualization Overview Basic Plotting Histograms, Lines, Scatter and Box Plots Matplotlib and Seaborn

Mapping Values with map()

Learn how to transform Series values using dictionaries, functions and lambda expressions with Pandas map().

1. What is map() in Pandas?

The map() method is used to transform each value in a Pandas Series. It can replace existing values with new values by using a dictionary, another mapping Series or a function.

It is especially useful when converting codes into readable labels, replacing categories, or applying a simple transformation to individual values.

import pandas as pd

numbers = pd.Series([1, 2, 3, 4])

result = numbers.map({
    1: "One",
    2: "Two",
    3: "Three",
    4: "Four"
})

print(result)

Output

0      One
1      Two
2    Three
3     Four
dtype: object

2. Syntax of map()

Series.map(arg, na_action=None)
Parameter Description
arg A dictionary, Series or function used to map or transform values.
na_action Use "ignore" to preserve missing values without passing them to the mapping function.
Remember: map() operates on a Series. To transform several DataFrame columns or perform row-wise calculations, consider other Pandas methods.

3. Mapping Values Using a Dictionary

A dictionary defines the relationship between original values and replacement values. This is one of the most common uses of map().

students = pd.Series([
    "A", "B", "A", "C", "B"
])

grade_names = {
    "A": "Excellent",
    "B": "Good",
    "C": "Average"
}

result = students.map(grade_names)

print(result)

Output

0    Excellent
1         Good
2    Excellent
3      Average
4         Good
dtype: object

Every value is looked up in the dictionary. The original Series remains unchanged unless you assign the result back to the same variable or column.

4. What Happens When a Key Is Missing?

When a dictionary does not contain a key for an existing non-missing value, map() returns a missing value for that item.

colors = pd.Series([
    "red", "blue", "green"
])

color_codes = {
    "red": "#FF0000",
    "blue": "#0000FF"
}

result = colors.map(color_codes)

print(result)

Output

0    #FF0000
1    #0000FF
2        NaN
dtype: object
Important: The value "green" has no matching dictionary entry, so its mapped result is missing. Check dictionary keys carefully before using the result in calculations or saving data.

5. Mapping Values Using a Function

You can pass a Python function to map() when you need to calculate a new value from each Series element.

prices = pd.Series([100, 200, 300, 400])

def add_tax(price):
    return price * 1.18

result = prices.map(add_tax)

print(result)

Output

0    118.0
1    236.0
2    354.0
3    472.0
dtype: float64

In this example, each price is multiplied by 1.18 to calculate an illustrative price including 18% tax.

6. Using Lambda Expressions

A lambda expression is a compact way to define a small function directly inside map().

numbers = pd.Series([10, 20, 30, 40])

result = numbers.map(lambda value: value * 2)

print(result)

Output

0    20
1    40
2    60
3    80
dtype: int64

Lambda expressions work well for short transformations. For complex conditions or reusable business rules, a named function is usually easier to understand.

7. Mapping Student Results

The following example converts numeric scores into descriptive performance categories.

marks = pd.Series([95, 72, 48, 85, 33])

def get_result(mark):
    if mark >= 80:
        return "Distinction"
    elif mark >= 50:
        return "Pass"
    return "Fail"

result = marks.map(get_result)

print(result)

Output

0    Distinction
1           Pass
2           Fail
3    Distinction
4           Fail
dtype: object

8. Mapping DataFrame Columns

You can use map() on a DataFrame column to replace codes with readable labels.

df = pd.DataFrame({
    "Name": ["Amit", "Neha", "Raj"],
    "Department": ["IT", "HR", "IT"]
})

department_names = {
    "IT": "Information Technology",
    "HR": "Human Resources"
}

df["Department"] = df["Department"].map(
    department_names
)

print(df)

Output

   Name               Department
0  Amit  Information Technology
1  Neha         Human Resources
2   Raj  Information Technology
Practical use: This technique is useful for converting department codes, status codes, product categories and other coded values into readable text.

9. Handling Missing Values

When a Series contains missing values, you can use na_action="ignore" to prevent missing values from being passed to a mapping function.

values = pd.Series([10, None, 30, 40])

result = values.map(
    lambda value: value * 2,
    na_action="ignore"
)

print(result)

Output

0    20.0
1     NaN
2    60.0
3    80.0
dtype: float64

Missing values remain missing. This option does not fill them with a default value. Use fillna() if your application requires a replacement.

10. map() vs apply() vs replace()

Method Primary purpose Example
map() Map or transform individual Series values. s.map({"A": "Active"})
apply() Apply a function to Series values or DataFrame rows and columns. s.apply(str.upper)
replace() Replace specified values while generally preserving unmatched values. s.replace({"A": "Active"})
Choose carefully: Dictionary-based map() can introduce missing values for unmatched keys. If unmatched values should stay unchanged, replace() may be more appropriate.

11. Common Mistakes and Best Practices

Common Mistakes

  • Using map() on an entire DataFrame instead of a Series.
  • Forgetting that unmatched dictionary keys can produce missing values.
  • Using inconsistent capitalization in dictionary keys.
  • Overwriting the original column unintentionally.
  • Writing complicated logic inside a long lambda expression.

Best Practices

  • Validate mapping keys before transforming data.
  • Use descriptive dictionary names.
  • Check missing values after mapping.
  • Use named functions for complex transformations.
  • Choose map(), apply() or replace() according to the requirement.

12. Practice Exercises

Exercise 1: Convert Status Codes

Convert the values 1, 2 and 3 into Pending, Approved and Rejected using a dictionary and map().

status = pd.Series([1, 2, 3, 1, 2])

# Write your solution below

Exercise 2: Apply a Discount

Use a lambda expression to reduce every price by 10%.

prices = pd.Series([100, 250, 500, 1000])

# Write your solution below

Exercise 3: Map Department Names

Convert the department codes IT, HR and Finance into descriptive names.

departments = pd.Series([
    "IT", "HR", "Finance", "IT"
])

# Write your solution below

Summary

Pandas map() transforms individual values in a Series using a dictionary, Series, or function. It is useful for replacing coded values, converting categories, and performing simple transformations.

Remember that unmatched dictionary keys generally produce missing values. Handle missing data carefully and select apply() or replace() when those methods better match the task.

Prefer map() for value mapping, validate your mapping keys, and check the output before updating your original DataFrame column.