How to use pandas.Series.map() [Practical Examples]

How to use pandas.Series.map() [Practical Examples]

Introduction to pandas.Series.map()

Pandas supportselement-wise operations just like NumPy (after all,pd.Seriesstores their data usingnp.array). For example, it is possible to apply transformation very easily on bothpd.Seriesandpd.DataFrame:

np.log(df.sys_initial)  # Logarithm of a series
df.sys_initial ** 2     # Square a series
np.log(df)              # Logarithm of a dataframe
df ** 2                 # Square of a dataframe

Thepd.Series.map method can be used to execute a function to each value and return apd.Seriescontaining each result. We can pass theSeriesto Python’s built-indictfunction to create a dictionary.

Themap()method is similar to theapplymethod as it helps in making elementwise changes that have been defined by functions. However, in addition, themapfunction also accepts a series or dictionary to define these elementwise changes.

Pandas maps theSeries’ index labels and values to the dictionary’s keys and values:

Syntax:

Series.map(arg, na_action=None)

Parameters:

  • arg - It will represents the Series data as input.
  • na_action - represents the action on NaN values.
NOTE
The first Series column will be an index to second mapped Series column

Example-1: map() two Panda Series

We are going to map() two Series with out any parameters.

Syntax:

new_series=old_series.map(values)

Example:

This method performs the mapping by first matching the values of the outerSerieswith the index labels of the innerSeries. It then returns a newSeries, with the index labels of the outerSeriesbut the values from the innerSeries.

# import the module
import pandas as pd

# Create Series x
x = pd.Series({"one": 1, "two": 2, "three": 3})

# Create Series y
y = pd.Series({1: "a", 2: "b", 3: "c"})

# Print x series
print(x)
print("====================")

# Print y series
print(y)
print("====================")

# map the labels in the index of x to the values of y
print(x.map(y))

Output:

one      1
two      2
three    3
dtype: int64
====================
1    a
2    b
3    c
dtype: object
====================
one      a
two      b
three    c
dtype: object

Example-2: map() Panda Series with Dictionary

As we mentioned earlier, we can also map() a Series with a Dictionary. So I will update my previous code, and convert the Panda Series into Dictionary using to_dict() function

# import the module
import pandas as pd

# Create Series x
x = pd.Series({"one": 1, "two": 2, "three": 3})

# Create Series y and convert it to dictionary
y = pd.Series({1: "a", 2: "b", 3: "c"}).to_dict()

# Print x series
print(x)
print("====================")

# Print y series
print(y)
print("====================")

# map the labels in the index of x to the values of y
print(x.map(y))

Output:

one      1
two      2
three    3
dtype: int64
====================
{1: 'a', 2: 'b', 3: 'c'}
====================
one      a
two      b
three    c
dtype: object

Example-3: Handle missing values during map() with na_action

The na_action='ignore' or na_action=None can be used to avoid applying map() function on missing values and keep them as NaN. Let’s update our code and remove one of the key value pair from the dictionary and try to perform map operation:

# import the module
import pandas as pd

# Create Series x
x = pd.Series({"one": 1, "two": 2, "three": 3})

# Create Series y
y = pd.Series({1: "a", 2: "b"})

# Print x series
print(x)
print("====================")

# Print y series
print(y)
print("====================")

# map the labels in the index of x to the values of y
print(x.map(y, na_action='ignore'))

Output

As you can see, we only had two elements in our dictionary so for the third key, map() has added as NaN:

one      1
two      2
three    3
dtype: int64
====================
1    a
2    b
dtype: object
====================
one        a
two        b
three    NaN
dtype: object

Can we use map() with Pandas DataFrame?

map() can use not only a function, but also a dictionary or another series. This method doesn’t exist on pandas.DataFrame objects.

We will update our code to use pandas DataFrame along with Series:

# import the module
import pandas as pd

# Create Series x
x = pd.Series({"one": 1, "two": 2, "three": 3})

# Create pandas DataFrame
y = pd.DataFrame([1, 2, 3])

# map the labels in the index of y to the values of x
print(y.map(x, na_action='ignore'))

As you can see from the output, we have got an exception:

Traceback (most recent call last):
  File "<string>", line 11, in <module>
  File "/usr/local/lib/python3.8/dist-packages/pandas/core/generic.py", line 5487, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'map'

Summary

One of the basic tasks in data transformations is the mapping of a set of values to another set. Pandas provides a generic ability to map values using a lookup table (via a Python dictionary or a pandasSeries) using the.map()method.

This method performs the mapping by first matching the values of the outerSerieswith the index labels of the innerSeries. It then returns a newSeries, with the index labels of the outerSeriesbut the values from the innerSeries.

As with other alignment operations, if pandas does not find a map between the value of the outerSeriesand an index label of the innerSeries, it fills the value withNaN.


References

Pandas - map() series

Deepak Prasad

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.