Pandas Difference Between map, applymap and apply Methods

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:January 27, 2023

What is the difference between map(), applymap() and apply() methods in pandas? – In padas, all these methods are used to perform either to modify the DataFrame or Series. map() is a method of Series, applymap() is a method of DataFrame, and apply() is defined in both DataFrame and Series.

In this pandas article, I will explain the differences between map()applymap() and apply() methods and their similarities with usages using examples.

1. Difference Between map() vs applymap() vs apply() methods

The main advantage of pandas is to manipulate data (transformations) and apply analytics on the data, all these map(), applymap() and apply() methods are used to modify the data however there are differences between these and their usage are slightly different.

1.1 pandas.Series.map()

  • This method defined only in Series and not present in DataFrame.
  • map() accepts dicts, Series, or callable
  • You can use this to perform operations on a specific column of a DataFrame as each column in a DataFrame is Series.
  • map() when passed a dictionary/Series will map elements based on the keys in that dictionary/Series. Missing values will be recorded as NaN in the output.
  • Series.map() operate on one element at time

1.2 pandas.DataFrame.applymap()

  • Pandas DataFrame.applymap() method is defined only in DataFrame.
  • Accept callables only.
  • applymap() is elementwise for DataFrames.
  • applymap() performs better than apply().
  • applymap() operates on one element at time

1.3 pandas.Series.apply() & pandas.DataFrame.apply()

  • This method defined in both Series and DataFrame
  • Accept callables only
  • apply() also works elementwise but is suited to more complex operations and aggregation.
  • DataFrame.apply() operates on entire rows or columns at a time.
  • Series.apply() operate on one element at time

2. Quick Examples of Difference Between map, applymap and apply

If you are in a hurry, below are some of the quick examples and usage of map(), applymap() and apply() in pandas DataFrame.


# Below are the quick examples

# Use Series.map() Method
df["Col 1"]=df["Col 1"].map(lambda x: x/100)

# Use Pandas Series.apply() Function to Single Column
df["A"]=df["A"].apply(lambda x: x/10)

# Use DataFrame.apply() method
df2 = df.apply(lambda x: x/10)

# Use Pandas DataFrame.applymap() method
df2 = df.applymap(lambda a: a*10)

# Use DataFrame.applymap() method
df2 = df.applymap(lambda a: str(a)+".00")

Now, let’s create a DataFrame with a few rows and columns, execute these examples and validate results. Our DataFrame contains column names A, B, and C.


import pandas as pd
import numpy as np
data = [(3,5,7), (2,4,6),(5,8,9)]
df = pd.DataFrame(data, columns = ['A','B','C'])
print(df)

Yields below output.


# Output:
   A  B  C
0  3  5  7
1  2  4  6
2  5  8  9

3. Use Pandas Series.apply() Function to Single Column

In order to update a single DataFrame column by applying some transformations use Series.apply() mehtod. On the below example df[‘A’] returns a Series object.


# Use Pandas DataFrame.apply() Function to Single Column.
df["A"]=df["A"].apply(lambda x: x/10)
print(df)

Yields below output.


# Output:
     A  B  C
0  0.3  5  7
1  0.2  4  6
2  0.5  8  9

4. Use DataFrame.apply() Function to Each Column

You can also apply a lambda expression, the pandas.DataFrmae.apply() is used to apply a transformation on all columns using lambda expression. The Below example, division 10 to all column values.


# Use DataFrame.apply() method
df2 = df.apply(lambda x: x/10)
print(df2)

Yields below output.


# Output:
     A    B    C
0  0.3  0.5  0.7
1  0.2  0.4  0.6
2  0.5  0.8  0.9

5. Use Pandas DataFrame.applymap() Method

DataFrame.applymap() is another approach to update all DataFrame columns at a time, similar to DataFrame.app() method.


# Use Pandas DataFrame.applymap() method
df2 = df.applymap(lambda a: a*10)
print(df2)

Yields below output.


# Output:
    A   B   C
0  30  50  70
1  20  40  60
2  50  80  90

Besides mathematical operations, you can also perform other operations to the elements of the DataFrame. It appends .00 at the end of each element in the DataFrame df.


# Use DataFrame.applymap() method
df2 = df.applymap(lambda a: str(a)+".00")
print(df2)

Yields below output.


# Output:
      A     B     C
0  3.00  5.00  7.00
1  2.00  4.00  6.00
2  5.00  8.00  9.00

6. Use Series.map() Method

You can only use the Series.map() method with the particular column of a DataFrame.


# Use DataFrame.map() Method
df["Col 1"]=df["Col 1"].map(lambda x: x/100)
print(df)

Yields below output.


# Output:
   Col 1  Col 2  Col 3
A    0.2     32     65
B    0.3     55     78
C    0.5     85     80
D    0.7     54     97

7. Complete Example For Difference Between map, applymap and apply


import pandas as pd
data = [(3,5,7), (2,4,6),(5,8,9)]
df = pd.DataFrame(data, columns = ['A','B','C'])
print(df)

# Use Pandas DataFrameapply() Function to Single Column
df["A"]=df["A"].apply(lambda x: x/10)
print(df)

# Use DataFrame.apply() method
df2 = df.apply(lambda x: x/10)
print(df2)

# Use Pandas DataFrame.applymap() method
df2 = df.applymap(lambda a: a*10)
print(df2)

# Use DataFrame.applymap() method
df2 = df.applymap(lambda a: str(a)+".00")
print(df2)

# Use DataFrame.map() Method
df["Col 1"]=df["Col 1"].map(lambda x: x/100)
print(df)

Conclusion

In this article, you have learned the difference between Series.map(), DataFrame.applymap(), Series.apply() and DataFrame.apply() methods of pandas with examples.

Happy Learning !!

References

Leave a Reply

You are currently viewing Pandas Difference Between map, applymap and apply Methods