How to Use the map() Function in Pandas


Often you may want to apply a particular function to each element in a pandas DataFrame.

For example, you may want to square each value in a DataFrame or divide each element by a particular value.

The most efficient way to do so is by using the map() function, which uses the following syntax:

pandas.map(func, na_action=None, …)

where:

  • func: The function to apply to each element
  • na_action: How to handle NaN values

Note: Typically we use the lambda statement to apply functions to each element in a pandas DataFrame, which the following example will illustrate.

The following example shows how to use the map() function in practice with a pandas DataFrame.

Example: How to Use map() Function in Pandas

Suppose we create the following pandas DataFrame that contains information about various basketball players:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'player': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
                   'points': [8, 12, 14, 14, 18, 15, 39, 24, 28],
                   'assists': [4, 3, 3, 2, 0, 3, 2, 5, 12]})

#view DataFrame
print(df)

  player  points  assists
0      A       8        4
1      B      12        3
2      C      14        3
3      D      14        2
4      E      18        0
5      F      15        3
6      G      39        2
7      H      24        5
8      I      28       12

The DataFrame contains information about the names of various basketball players along with the number of points they scored and their total assists.

Suppose that we would like to multiply each value in the assists column of the DataFrame by 5.

We can use the following syntax to do so:

#multiply each value in 'assists' column by 5
df['assists'].map(lambda x: x*5)

0    20
1    15
2    15
3    10
4     0
5    15
6    10
7    25
8    60
Name: assists, dtype: int64

The map() function returns the value of each element in the assists column of the DataFrame multiplied by 5.

For example:

  • The first value returns 4 * 5 = 20
  • The second value returns 3 * 5 = 15
  • The third value returns 3 * 5 = 15

And so on.

Note: The last line in the output displays the dtype of the resulting Series, which is an integer in this example.

Note that we can also use a more complex function in the map() function if we’d like.

For example, we can use the following syntax to multiply each value in the assists column by 5 and then divide by 1.2:

#multiply each value in 'assists' column by 5 and then divide by 1.2
df['assists'].map(lambda x: (x*5) / 1.2)

0    16.666667
1    12.500000
2    12.500000
3     8.333333
4     0.000000
5    12.500000
6     8.333333
7    20.833333
8    50.000000
Name: assists, dtype: float64

The map() function returns the result of the function for each value in the assists column.

For example:

  • The first value returns 4 * 5 /1.2  = 16.67
  • The second value returns 3 * 5 / 1.2 = 12.5
  • The third value returns 3 * 5 / 1.2 = 12.5

And so on.

Note that we can also store the results of the function in a new column in the DataFrame if we’d like.

For example, we can use the following syntax to multiply each value in the assists column by 5 and then store the results in a new column named assists5:

#multiply each value in 'assists' column by 5
df['assists5'] = df['assists'].map(lambda x: x*5)

#view updated DataFrame
print(df)

  player  points  assists  assists5
0      A       8        4        20
1      B      12        3        15
2      C      14        3        15
3      D      14        2        10
4      E      18        0         0
5      F      15        3        15
6      G      39        2        10
7      H      24        5        25
8      I      28       12        60

The results of the map() function are now stored in a new column named assists5.

Note: You can find the complete documentation for the map() function in pandas here.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Use Pandas apply() inplace
How to Select Only Numeric Columns in Pandas
Pandas: How to Fill NaN Values Using a Dictionary

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *