Open In App

Using Apply in Pandas Lambda functions with multiple if statements

Last Updated : 20 Jun, 2025
Comments
Improve
Suggest changes
3 Likes
Like
Report

We are given a DataFrame and our task is to assign values to a new column based on multiple conditions in an existing column. While the lambda function is good for simple conditions, it struggles with multiple if-elif-else logic. To overcome this, we can define a custom function and use apply() to handle more complex branching logic. For example, if we want to label scores above 8 as "No need", scores between 5 and 7 as "Hold decision", and below 5 as "Need", we can’t do this with a single lambda expression directly.

Creating the DataFrame

Python
import pandas as pd

df = pd.DataFrame({
    'name': ['John', 'Jack', 'Shri', 'Krishna', 'Smith', 'Tessa'],
    'marks': [5, 3, 9, 10, 6, 3]
})
print(df)

Output
      name  marks
0     John      5
1     Jack      3
2     Shri      9
3  Krishna     10
4    Smith      6
5    Tessa      3

Explanation:

  • We create a simple DataFrame with student names and their marks.
  • This will be used to classify students using custom logic.

Using Lambda with Single If Condition

Lambda functions in apply() are ideal for simple conditions such as pass/fail classification.

Python
import pandas as pd

df = pd.DataFrame({'Name': ['John', 'Jack', 'Shri',
                            'Krishna', 'Smith', 'Tessa'],
                   'Maths': [5, 3, 9, 10, 6, 3]})

# Adding the result column
df['Result'] = df['Maths'].apply(lambda x: 'Pass' if x>=5 else 'Fail')

print(df)

Output
      Name  Maths Result
0     John      5   Pass
1     Jack      3   Fail
2     Shri      9   Pass
3  Krishna     10   Pass
4    Smith      6   Pass
5    Tessa      3   Fail

Explanation:

  • A lambda is used to assign "Pass" if marks are ≥5, otherwise "Fail".
  • This works because the condition is straightforward and uses a simple if-else.

Why Lambda Fails for Multiple Conditions

If we try to add multiple if-elif-else blocks inside a lambda expression then it causes syntax errors. Example that will break and cause syntax error:

Python
df['spl_class'] = df['marks'].apply(lambda x: "No Need" if x > 8 elif x >= 5: "Hold" else "Need")

Output:

SyntaxError: invalid syntax

Explanation:

  • Lambda supports only a single if-else, not chained conditions.
  • You can't use elif inside a lambda like in regular Python.

Using a Custom Function with apply() for Multiple Conditions

To use multiple conditional checks, define a function and pass it to apply().

Python
import pandas as pd

df = pd.DataFrame({'Name': ['John', 'Jack', 'Shri', 
                            'Krishna', 'Smith', 'Tessa'],
                   'marks': [5, 3, 9, 10, 6, 3]})

def classify(x):
    if x > 8:
        return 'No need'
    elif 5 <= x <= 7:
        return 'Hold decision'
    else:
        return 'Need'

df['spl_class'] = df['marks'].apply(classify)
print(df)

Output:

Name marks spl_class

0 John 5 Hold decision

1 Jack 3 Need

2 Shri 9 No need

3 Krishna 10 No need

4 Smith 6 Hold decision

5 Tessa 3 Need

Explanation:

1. The function classify() handles three conditions:

  • More than 8 then "No need"
  • Between 5 and 7 then "Hold decision"
  • Less than 5 then "Need"

2. This function is passed to apply(), which applies it row-wise to the marks column.

Using numpy.select() for Multiple Conditions

Instead of using apply(), we can also use np.select() to apply multiple conditions without writing a custom function.

Python
import numpy as np
import pandas as pd

df = pd.DataFrame({
    'name': ['John', 'Jack', 'Shri', 'Krishna', 'Smith', 'Tessa'],
    'marks': [5, 3, 9, 10, 6, 3]
})

cond = [
    df['marks'] > 8,
    (df['marks'] >= 5) & (df['marks'] <= 7),
    df['marks'] < 5
]

choice = ['No need', 'Hold decision', 'Need']

df['spl_class_np'] = np.select(cond, choice, default='Unknown')
print(df)

Output:

name marks spl_class_np

0 John 5 Hold decision

1 Jack 3 Need

2 Shri 9 No need

3 Krishna 10 No need

4 Smith 6 Hold decision

5 Tessa 3 Need

Explanation:

  • We define a list of boolean conditions where each condition maps to a label.
  • np.select() applies these conditions efficiently and assigns the corresponding values.

Explore