No more confusion between apply(), map() and applymap()

Sayantanee Sen
Analytics Vidhya
Published in
3 min readJul 13, 2021
Photo by Pablo García Saldaña on Unsplash

Do you find yourself often confused between Python’s apply() , map() and applymap() functions? Well, let us spend few minutes to understand these functions.

What apply() does?

The pandas apply() function operates on both dataframes and series. We can use it on either columns of the dataframes (axis=1) or on rows of the dataframes (axis=0). Let’s take an example to understand this better.

df = pd.DataFrame({ 'Name': ["Homer","Marge","Bart","Lisa","Maggie"],'Surname': ["Simpson","Simpson","Simpson","Simpson","Simpson"]})

To change the Surname “Simpson” into upper case, we operate on column level.

df['Surname']=df['Surname'].apply(lambda x: x.upper())

To change any one row into upper case, we operate on the index level.

df.loc[0]=df.loc[0].apply(lambda x: x.upper())

We can also use apply() on the entire dataframe.

df=df.apply(lambda x: x.str.upper())

What map() does?

map() can only operate on series and not on whole dataframes. Hence we can either replace all the values of a series or of a column in the dataframe or of a homogeneous dataframe row, using the map() functionality.

In our example, since we passing the columns and rows as a series of homogenous strings, map() function can also be substituted here.

df['Surname']=df['Surname'].map(lambda x: x.upper())
df.loc[0]=df.loc[0].map(lambda x: x.upper())

However, if we add an age column in our dataset, neither map() nor apply() can be used to do row wise operations, as we are performing a string operation.

df['age']=[36,34,10,8,1]
df.loc[0]=df.loc[0].map(lambda x: x.upper())
AttributeError: 'numpy.int64' object has no attribute 'upper'

This can be fixed with a small condition though!

df.loc[0]=df.loc[0].apply(lambda x: x.upper() if type(x)==str else x)

Unlike apply(), map() won’t work on a dataframe even if you have all columns of the same data type.

What applymap() does?

Finally applymap() operates on the entire dataframe and performs element wise operations.

df=df.applymap(lambda x: x.upper())

Happy Coding!

--

--