Pandas: How to Use Apply & Lambda Together


You can use the following basic syntax to apply a lambda function to a pandas DataFrame:

df['col'] = df['col'].apply(lambda x: 'value1' if x < 20 else 'value2')

The following examples show how to use this syntax in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   'points': [18, 22, 19, 14, 14, 11, 20, 28],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4]})

#view DataFrame
print(df)

  team  points  assists
0    A      18        5
1    B      22        7
2    C      19        7
3    D      14        9
4    E      14       12
5    F      11        9
6    G      20        9
7    H      28        4

Example 1: Use Apply and Lambda to Create New Column

The following code shows how to use apply and lambda to create a new column whose values are dependent on the values of an existing column:

#create new column called 'status'
df['status'] = df['points'].apply(lambda x: 'Bad' if x < 20 else 'Good')

#view updated DataFrame
print(df)

  team  points  assists status
0    A      18        5    Bad
1    B      22        7   Good
2    C      19        7    Bad
3    D      14        9    Bad
4    E      14       12    Bad
5    F      11        9    Bad
6    G      20        9   Good
7    H      28        4   Good

In this example, we created a new column called status that took on the following values:

  • Bad‘ if the value in the points column was less than 20.
  • Good‘ if the value in the points column was greater than or equal to 20.

Example 2: Use Apply and Lambda to Modify Existing Column

The following code shows how to use apply and lambda to modifying an existing column in the DataFrame:

#modify existing 'points' column
df['points'] = df['points'].apply(lambda x: x/2 if x < 20 else x*2)

#view updated DataFrame
print(df)

  team  points  assists
0    A     9.0        5
1    B    44.0        7
2    C     9.5        7
3    D     7.0        9
4    E     7.0       12
5    F     5.5        9
6    G    40.0        9
7    H    56.0        4

In this example, we modified the values in the existing points column by using the following rule in the lambda function:

  • If the value is less than 20, divide the value by 2.
  • If the value is greater than or equal to 20, multiply the value by 2.

Using this lambda function, we were able to modify the values in the existing points column.

Additional Resources

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

How to Apply Function to Pandas Groupby
How to Fill NaN with Values from Another Column in Pandas

Leave a Reply

Your email address will not be published.