The pandas’ map()
method is used to map dictionaries or functions to pandas series or dataframe columns. In this article, we will discuss different ways to map a function, series, or dictionary to a series or column in a pandas dataframe.
- The map() Method
- Pandas Map Function to Series in Python
- Map Function to a Column in Python
- Map Dictionary to a Series in Python
- Pandas Map Dictionary to a Dataframe Column
- Map Series to Another Series in Python
- Map Series to a Column in the Pandas Dataframe
- Pandas Apply Lambda Function to a Series
- Apply Lambda Function to Dataframe Column
- Conclusion
The map() Method
The map()
method, when invoked on a series or dataframe column, takes a function, series, or a python dictionary as its input argument. It has the following syntax.
Series.map(arg, na_action=None)
Here,
- The
arg
parameter takes a function, python dictionary, or a series as its input argument.- If we pass a function to the
map()
method, it applies the function to the elements of the series or pandas dataframe column and returns the modified series or column. - When we pass a dictionary to the
map()
method, it replaces the keys of the dictionary with the values of the dictionary in the dataframe column or the series. - If you want to pass a series as input to the
map()
method, the series should contain a custom index. Themap()
method replaces the index of the input series with the values of the series in the original dataframe column or the series.
- If we pass a function to the
- The
na_action
parameter is used to handle NaN values in the series or dataframe column. If you want to copy the NaN value directly from the original series to the output series, you can set thena_action
parameter to“ignore”
. If thena_action
parameter is set to None, which is its default value, the function passed to thena_action
parameter should handle the NaN values explicitly.
Pandas Map Function to Series in Python
To map a function to a pandas series, you can pass the function to the map()
method after invoking it on the series as shown below.
import pandas as pd
import numpy as np
numbers=[100,90,80,90,70,100,60]
series=pd.Series(numbers)
print("The series is:")
print(series)
newSeries=series.map(np.sqrt)
print("The updated series is:")
print(newSeries)
Output:
The series is:
0 100
1 90
2 80
3 90
4 70
5 100
6 60
dtype: int64
The updated series is:
0 10.000000
1 9.486833
2 8.944272
3 9.486833
4 8.366600
5 10.000000
6 7.745967
dtype: float64
In this example, we have first created a pandas series. Then, we invoked the map()
method on the series and passed the numpy.sqrt
function as its input argument. In the output, you can observe that the sqrt
function is applied to all the elements of the input series.
Instead of passing an in-built function, you can also pass a user-defined function to the map()
method as shown below.
import pandas as pd
import numpy as np
def fun1(x):
nameDict={100:"Hundred", 90:"Ninety", 80:"Eighty", 70:"Seventy", 60:"Sixty"}
if x in nameDict:
return nameDict[x]
else:
return x
numbers=[100,90,80,90,70,100,60]
series=pd.Series(numbers)
print("The series is:")
print(series)
newSeries=series.map(fun1)
print("The updated series is:")
print(newSeries)
Output:
The series is:
0 100
1 90
2 80
3 90
4 70
5 100
6 60
dtype: int64
The updated series is:
0 Hundred
1 Ninety
2 Eighty
3 Ninety
4 Seventy
5 Hundred
6 Sixty
dtype: object
In the above example, we have defined the function fun1()
that maps the numbers to their alphabetical names. Then, we passed the function to the map()
method by invoking it on the series. After execution of the map()
method, we get the output series.
If there are nan values in the input series, the program might run into an error if the input function doesn’t handle NaN values. For instance, consider the following example.
import pandas as pd
import numpy as np
def fun1(x):
nameDict={100:"Hundred", 90:"Ninety", 80:"Eighty", 70:"Seventy", 60:"Sixty"}
if x in nameDict:
return nameDict[x]
else:
return int(x)
numbers=[100,90,80,np.nan,70,100,pd.NA]
series=pd.Series(numbers)
print("The series is:")
print(series)
newSeries=series.map(fun1)
print("The updated series is:")
print(newSeries)
Output:
ValueError: cannot convert float NaN to integer
In this example, the function fun1()
cannot process a NaN value. Hence, the program runs into the Python ValueError exception.
To avoid the program getting into error, you can ignore the NaN value by setting the na_action
parameter to “ignore”
as shown below.
import pandas as pd
import numpy as np
def fun1(x):
nameDict={100:"Hundred", 90:"Ninety", 80:"Eighty", 70:"Seventy", 60:"Sixty"}
if x in nameDict:
return nameDict[x]
else:
return int(x)
numbers=[100,90,80,np.nan,70,100,pd.NA]
series=pd.Series(numbers)
print("The series is:")
print(series)
newSeries=series.map(fun1,na_action="ignore")
print("The updated series is:")
print(newSeries)
Output:
The series is:
0 100
1 90
2 80
3 NaN
4 70
5 100
6 <NA>
dtype: object
The updated series is:
0 Hundred
1 Ninety
2 Eighty
3 NaN
4 Seventy
5 Hundred
6 <NA>
dtype: object
In this example, the input series contains NaN values. Hence, when we set the na_action
parameter to "ignore"
, the NaN values are mapped directly to the output series from the input series.
Map Function to a Column in Python
A column in a pandas dataframe is essentially a series object. Hence, you can map a function to a pandas dataframe column using the map()
method as shown below.
import pandas as pd
import numpy as np
def fun1(x):
nameDict={100:"Hundred", 90:"Ninety", 80:"Eighty", 70:"Seventy", 60:"Sixty"}
if x in nameDict:
return nameDict[x]
else:
return x
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
{"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
{"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
{"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
{"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
{"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
df["Maths"]=df["Maths"].map(fun1)
print("The updated dataframe is:")
print(df)
Output:
The input dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
1 2 80 100 90
2 3 90 80 70
3 4 100 100 90
4 5 90 90 80
5 6 80 70 70
The updated dataframe is:
Roll Maths Physics Chemistry
0 1 Hundred 80 90
1 2 Eighty 100 90
2 3 Ninety 80 70
3 4 Hundred 100 90
4 5 Ninety 90 80
5 6 Eighty 70 70
If there are NaN values in the dataframe column, you can set the na_action
parameter to “ignore”
to avoid any error.
Map Dictionary to a Series in Python
Just like a function, you can also map a python dictionary to a pandas series. For this, you need to pass the dictionary to the map()
method as its input argument. After execution, we get a new series in which the elements of the original series which are present in the dictionary as keys are replaced by the associated values in the dictionary.
You can observe this in the following example.
import pandas as pd
import numpy as np
nameDict={100:"Hundred", 90:"Ninety", 80:"Eighty", 70:"Seventy", 60:"Sixty"}
numbers=[100,90,80,90,70,100,60]
series=pd.Series(numbers)
print("The series is:")
print(series)
newSeries=series.map(nameDict)
print("The updated series is:")
print(newSeries)
Output:
The series is:
0 100
1 90
2 80
3 90
4 70
5 100
6 60
dtype: int64
The updated series is:
0 Hundred
1 Ninety
2 Eighty
3 Ninety
4 Seventy
5 Hundred
6 Sixty
dtype: object
In this example, we have passed a python dictionary instead of a function to the map()
method. Hence, the map()
method maps the elements of the series to values of the dictionary based on the keys.
If the dictionary doesn’t contain all the elements of the original series as its keys, the remaining elements are mapped to NaN values in the output series as shown below.
import pandas as pd
import numpy as np
nameDict={100:"Hundred", 70:"Seventy", 60:"Sixty"}
numbers=[100,90,80,90,70,100,60]
series=pd.Series(numbers)
print("The input series is:")
print(series)
newSeries=series.map(nameDict)
print("The updated series is:")
print(newSeries)
Output:
The input series is:
0 100
1 90
2 80
3 90
4 70
5 100
6 60
dtype: int64
The updated series is:
0 Hundred
1 NaN
2 NaN
3 NaN
4 Seventy
5 Hundred
6 Sixty
dtype: object
In this example, we don’t have mappings for the elements 80 and 90 in the series. Hence, the corresponding values in the output series are set to NaN.
Pandas Map Dictionary to a Dataframe Column
To map a dictionary to a dataframe column, you can pass the dictionary to the map()
method as shown below.
import pandas as pd
import numpy as np
nameDict={100:"Hundred", 90:"Ninety", 80:"Eighty", 70:"Seventy", 60:"Sixty"}
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
{"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
{"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
{"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
{"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
{"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
df["Maths"]=df["Maths"].map(nameDict)
print("The updated dataframe is:")
print(df)
Output:
The input dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
1 2 80 100 90
2 3 90 80 70
3 4 100 100 90
4 5 90 90 80
5 6 80 70 70
The updated dataframe is:
Roll Maths Physics Chemistry
0 1 Hundred 80 90
1 2 Eighty 100 90
2 3 Ninety 80 70
3 4 Hundred 100 90
4 5 Ninety 90 80
5 6 Eighty 70 70
Map Series to Another Series in Python
You can also map a series to another series using the map()
method.
For this, you need to define a new series with elements of the original series as its index and replacement values as the elements associated with each index. Then, you can pass the series to the map()
method to map it to the original series as shown below.
import pandas as pd
import numpy as np
nameDict={100:"Hundred", 90:"Ninety", 80:"Eighty", 70:"Seventy", 60:"Sixty"}
nameSeries=pd.Series(nameDict)
print("The series for mapping is")
print(nameSeries)
numbers=[100,90,80,90,70,100,60]
series=pd.Series(numbers)
print("The input series is:")
print(series)
newSeries=series.map(nameSeries)
print("The updated series is:")
print(newSeries)
Output:
The series for mapping is
100 Hundred
90 Ninety
80 Eighty
70 Seventy
60 Sixty
dtype: object
The input series is:
0 100
1 90
2 80
3 90
4 70
5 100
6 60
dtype: int64
The updated series is:
0 Hundred
1 Ninety
2 Eighty
3 Ninety
4 Seventy
5 Hundred
6 Sixty
dtype: object
In this example, the nameSeries
contains the elements of the input series as the index. Hence, when we pass nameSeries
to the map()
method, it maps the elements of the input series to the elements of nameSeries
based on the index values.
Map Series to a Column in the Pandas Dataframe
Just like a series, you can also map a series to a pandas dataframe column as shown below.
import pandas as pd
import numpy as np
nameDict={100:"Hundred", 90:"Ninety", 80:"Eighty", 70:"Seventy", 60:"Sixty"}
nameSeries=pd.Series(nameSeries)
print("The series for mapping is")
print(nameSeries)
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
{"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
{"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
{"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
{"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
{"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
df["Maths"]=df["Maths"].map(nameSeries)
print("The updated dataframe is:")
print(df)
Output:
The series for mapping is
100 Hundred
90 Ninety
80 Eighty
70 Seventy
60 Sixty
dtype: object
The input dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
1 2 80 100 90
2 3 90 80 70
3 4 100 100 90
4 5 90 90 80
5 6 80 70 70
The updated dataframe is:
Roll Maths Physics Chemistry
0 1 Hundred 80 90
1 2 Eighty 100 90
2 3 Ninety 80 70
3 4 Hundred 100 90
4 5 Ninety 90 80
5 6 Eighty 70 70
In this example, we have mapped the nameSeries
to the "Maths"
column in the pandas dataframe using the map()
method.
Pandas Apply Lambda Function to a Series
Lambda functions in Python are anonymous functions that have only a single statement. You can map a lambda function to a pandas series using the map()
method as shown in the following example.
import pandas as pd
import numpy as np
numbers=[100,90,80,90,70,100,60]
series=pd.Series(numbers)
print("The input series is:")
print(series)
newSeries=series.map(lambda x: x//10)
print("The updated series is:")
print(newSeries)
Output:
The input series is:
0 100
1 90
2 80
3 90
4 70
5 100
6 60
dtype: int64
The updated series is:
0 10
1 9
2 8
3 9
4 7
5 10
6 6
dtype: int64
In this example, we created a lambda function that takes a number as its input and returns its 10th factor. Then, we mapped the lambda function to the input series using the map()
method.
Apply Lambda Function to Dataframe Column
Instead of a series, you can also map a lambda function to a pandas dataframe column as shown below.
import pandas as pd
import numpy as np
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
{"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
{"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
{"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
{"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
{"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
df["Maths"]=df["Maths"].map(lambda x: x//10)
print("The updated dataframe is:")
print(df)
Output:
The input dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
1 2 80 100 90
2 3 90 80 70
3 4 100 100 90
4 5 90 90 80
5 6 80 70 70
The updated dataframe is:
Roll Maths Physics Chemistry
0 1 10 80 90
1 2 8 100 90
2 3 9 80 70
3 4 10 100 90
4 5 9 90 80
5 6 8 70 70
Conclusion
In this article, we have discussed different ways to map a function, dictionary, or series to a pandas series or dataframe column. We also discussed how to map a lambda function to a series or a dataframe column.
To learn more about python programming, you can read this article on how to sort a pandas dataframe. You might also like this article on how to drop columns from a pandas dataframe.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.