apply() vs map() vs applymap() in Pandas | Towards Data Science

Usually, we need to apply certain functions over DataFrame columns or rows in order to either update values or even create new columns. The most commonly used operations for doing so in pandas, are apply, map and applymap methods.
In today’s guide we are going to explore all three methods and understand how each of them works. Additionally, we will discuss when to use one over the other.
First, let’s create an example DataFrame that we will use throughout this article in order to demonstrate a few concepts that will help us highlight the difference between apply(), map() and applymap().
import pandas as pddf = pd.DataFrame(    [        (1, 521, True, 10.1, 'Hello'),        (2, 723, False, 54.2, 'Hey'),        (3, 123, False, 33.2, 'Howdy'),        (4, 641, True, 48.6, 'Hi'),        (5, 467, False, 98.1, 'Hey'),    ],    columns=['colA', 'colB', 'colC', 'colD', 'colE'])print(df)   colA  colB   colC  colD   colE0     1   521   True  10.1  Hello1     2   723  False  54.2    Hey2     3   123  False  33.2  Howdy3     4   641   True  48.6     Hi4     5   467  False  98.1    Hey
pandas.DataFrame.apply method is used to apply a function along the specified axis of the pandas DataFrame. apply() method operates on entire rows or columns at a time and is mostly suitable when it comes to applying functions that cannot be vectorised. Note that the input to the method must be a callable.
import numpy as npdf[['colA', 'colB']] = df[['colA', 'colB']].apply(np.sqrt)print(df)       colA       colB   colC  colD   colE0  1.000000  22.825424   True  10.1  Hello1  1.414214  26.888659  False  54.2    Hey2  1.732051  11.090537  False  33.2  Howdy3  2.000000  25.317978   True  48.6     Hi4  2.236068  21.610183  False  98.1    Hey
Additionally, the method can also be applied over pandas Series (see pandas.Series.apply).
pandas.Series.map method can applied only over pandas Series objects and is used to map the values of the Series based on the input which is used to substitute each value with the specified value that is derived from a dictionary, a function or even another Series object.
Note that the method operates over one element at a time and missing values will be denoted as NaN in the output.
df['colE'] = df['colE'].map({'Hello': 'Good Bye', 'Hey': 'Bye'})print(df)   colA  colB   colC  colD      colE0     1   521   True  10.1  Good Bye1     2   723  False  54.2       Bye2     3   123  False  33.2       NaN3     4   641   True  48.6       NaN4     5   467  False  98.1       Bye
Lastly, pandas.DataFrame.applymap method can only be applied over pandas DataFrame objects and is used to apply a specified function elementwise. The method accepts only callables and is mostly suitable when it comes to transforming values in multiple rows or columns.
df[['colA', 'colD']] = df[['colA', 'colD']].applymap(lambda x: x**2)print(df)   colA  colB   colC     colD   colE0     1   521   True   102.01  Hello1     4   723  False  2937.64    Hey2     9   123  False  1102.24  Howdy3    16   641   True  2361.96     Hi4    25   467  False  9623.61    Hey
In today’s short guide we discussed how apply(), map() and applymap() methods work in pandas. Additionally, we showcased how to use each of these methods and explored their main differences.
Sometimes, it may not be clear whether you need to use apply() or applymap() method and therefore, the best way forward is to simply test both methods and pick the one which is more efficient/faster.
Become a member and read every story on Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.
gmyrianthous.medium.com
You may also like