How pandas map works in Python? Best example

How pandas map works in Python? Best example
“`html

If you’ve ever worked with pandas in Python, you know it’s an incredibly powerful library for handling and analyzing data. One of its lesser-known yet powerful features is the map() function. In this article, I’ll walk you through how pandas.map() works, show the best use cases, and provide examples to make it clear.

Understanding pandas.map()

The map() function in pandas is primarily used to modify Series by applying a function, dictionary, or another mapping. It works only on pandas Series, not on DataFrames. Here’s the basic syntax:


Series.map(arg, na_action=None)
  • arg – Can be a function, dictionary, or Series used for mapping.
  • na_action – If set to ‘ignore’, it leaves NaN values unchanged.

How pandas.map() Works in Python? Best Example

Let’s dive into some examples to see how map() works in different scenarios.

1. Using map() with a Function

You can apply a function to each element in a pandas Series.


import pandas as pd

# Creating a Series
data = pd.Series([1, 2, 3, 4, 5])

# Applying a function using map()
result = data.map(lambda x: x * 2)

print(result)

Output:


0     2
1     4
2     6
3     8
4    10
dtype: int64

2. Using map() with a Dictionary

We can use a dictionary to replace specific values in a Series.


data = pd.Series(['A', 'B', 'C', 'A', 'B'])

# Dictionary for mapping
mapping_dict = {'A': 'Apple', 'B': 'Banana', 'C': 'Cherry'}

# Applying map()
result = data.map(mapping_dict)

print(result)

Output:


0    Apple
1    Banana
2    Cherry
3    Apple
4    Banana
dtype: object

This is incredibly useful when you want to replace categorical values in a dataset.

3. Using map() with Another Series

Pandas also allows us to map values from another Series.


names = pd.Series(['Alice', 'Bob', 'Charlie'])
scores = pd.Series({'Alice': 85, 'Bob': 92, 'Charlie': 78})

# Map values from another Series
result = names.map(scores)

print(result)

Output:


0    85
1    92
2    78
dtype: int64

It works similarly to a dictionary lookup, where values from names are used as keys to fetch values from scores.

4. Handling Missing Values with map()

What happens when a key is not found during mapping? Let’s check an example:


data = pd.Series(['A', 'B', 'X', 'C'])
mapping_dict = {'A': 'Apple', 'B': 'Banana', 'C': 'Cherry'}

# Applying map()
result = data.map(mapping_dict)

print(result)

Output:


0    Apple
1    Banana
2      NaN
3    Cherry
dtype: object

If a key is missing (like ‘X’ in this case), pandas will return NaN by default.

Comparison of map(), apply(), and replace()

While map() is powerful, it’s often compared to apply() and replace(). Here’s a quick comparison:

Function Best Used For Works On
map() Element-wise mapping (Series only) Series
apply() More complex functions, works on both Series and DataFrames Series & DataFrame
replace() Replacing multiple values at once Series & DataFrame

When Should You Use pandas.map()?

The map() function is extremely useful in several scenarios:

  • When you need to transform individual values in a pandas Series.
  • When mapping categorical values using a dictionary.
  • When performing simple element-wise operations.

Final Thoughts

Now that you’ve learned how pandas.map() works, you can confidently apply it in your data processing tasks. Whether you’re modifying values using functions, dictionaries, or even another Series, map() offers a clean and efficient solution.

“` Other interesting article: How pandas apply works in Python? Best example