How to Use Map() Apply() ApplyMap() Methods in Pandas
Table of Contents
In this article, we will talk about how to use map(), apply() and applymap() and when to use one over the other. These methods can apply certain processing over DataFrame Columns and update values or create new columns.
Relationship between three methods and DataFrame, Series
The relationship between these three methods and DataFrame, Series is organized as follows.
How to use map() method
pandas.Series.map will map values of some series based on some rules or input correspondence. When passed a dictionary
or Series
elements will be mapped based on the keys of dictionary
or Series
. Missing values will be converted to NaN.
map() is element-wise for Series.
For example, we can use map() method to map scores to grades as below.
Firstly, we prepare the data.
import pandas as pd
df = pd.DataFrame({
"name": ["Kevin", "Jack", "Mary", "Bob", "Robert", "Amy"],
"score": [66, 75, 80, 93, 77, 81],
"sex": ["M", "M", "F", "M", "M", "F"],
})
df
map() accepts dict
, Series
, or Callable
.
Use dict to Say hello to everyone with titles
For example if sex is ‘M’, use Mr. and sex is ‘F’ use Ms.
We specify a dict in which the mapping is defined to achieve this goal as below. If some values are not found in the dict, they will be converted to NaN.
df["say-hello"] = df["sex"].map({"M": "Hello, Mr.", "F": "Hello, Ms."})
df
Use Series to add nick name column
We have to define a Series for nick name and specify name column as its index, and use this Series in map().
nick_name = pd.Series(['K', 'J', 'M', 'B', 'R', 'A'], index=df["name"])
df["nick-name"] = df["name"].map(nick_name)
df
Use callable to add grade column
We can use a function or lambda expression in map() method to map a column to another column.
# The function that map score to grade
def get_grade(score):
# Firstly convert score to float type
try:
num = float(score)
if num < 60.0:
return "D"
if num < 80.0:
return "C"
if num < 90.0:
return "B"
else:
return "A"
except ValueError:
# If converting failed, return empty string
return ""
df["grade_func"] = df["score"].map(get_grade)
df
Of course we can use lambda expression for simple mapping.
df["say-hello-msg"] = df["name"].map(lambda x: f"Hello, {x}!")
df
How to use apply() method
pandas.DataFrame.apply can be used for DataFrame. It is used to apply a function (some converting or processing) along an axis of the DataFrame.
apply() also works elementwise but is more suited to complex operations and aggregation.
You can also read my another article about adding column to exising DataFrame.
apply() method is very suitable for processing over columns cannot be vectorised.
Pandas » How to Add Columns to an Existing DataFrame
https://thats-it-code.com/pandas/pandas__how-to-add-columns-to-an-existing-dataframe/
For example, we can get greeting message according to name and sex columns.
- Use apply() method of DataFrame df
- lambda or function can be specified, when using lambda expression
row
will be the whole row data. - Specify axis=1 when get columns according to multiple columns
df["greeting-msg"] = df.apply(lambda row: f"Welcome, {'Mr.' if row['sex'] == 'M' else 'Ms.'} {row['name']}", axis=1)
The apply() method can also be applied over Series.
def check_excellent(score):
# Firstly convert score to float type
try:
num = float(score)
if num > 90.0:
return "Excellent"
else:
return ""
except ValueError:
# If converting failed, return empty string
return ""
df["excellent"] = df["score"].apply(check_excellent)
df
How to use applymap() method
Lastly, we will talk about pandas.DataFrame.applymap method.
This method applies a function that accepts and returns a scalar to every element of a DataFrame. It can only be applied over pandas DataFrame.
Sometimes it is faster than apply() method.
For example, we can convert name and say-hello column to upper case.
df[["name", "say-hello"]] = df[["name", "say-hello"]].applymap(lambda x: x.upper())
df
Conclusion
- DataFrame
- apply() method is used to apply complicated processing over multiple columns or rows.
one column or multiple columns:axis=1
one row or multiple rows:axis=0
- applymap() method is used for element-wise operation over whole DataFrame.
- Series
- apply() method is used to apply complicated processing over the values of Series.
- map() method is used to mapping each value of Series to another value based on some rules or correspondence.