Open In App

Using Apply in Pandas Lambda functions with multiple if statements

Last Updated : 20 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to apply multiple if statements with lambda function in a pandas dataframe. Sometimes in the real world, we will need to apply more than one conditional statement to a dataframe to prepare the data for better analysis. 

We normally use lambda functions to apply any condition on a dataframe,

Syntax: lambda arguments: expression

An anonymous function which we can pass in instantly without defining a name or any thing like a full traditional function.

While we are using this lambda function we are limited with only one condition and an else condition. We cannot add multiple if statements like real python code. Now we can break those limitations and see how to add multiple if statements in the lambda function.

Creating Dataframe for demonestration:

Python3




# Importing the library
import pandas as pd
 
# dataframe
df = pd.DataFrame({'Name': ['John', 'Jack', 'Shri',
                            'Krishna', 'Smith', 'Tessa'],
                   'Maths': [5, 3, 9, 10, 6, 3]})
print(df)


Output:

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

If you need to classify the students based on their marks as pass or fail it is pretty straightforward to perform with a lambda function. 

For example,

syntax: df[ ‘Result’ ] = df[ ‘Maths’ ].apply( lambda x: ‘Pass’ if x>=5 else ‘Fail’ )

Python3




# Import the library
import pandas as pd
 
# dataframe
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

Adding Multiple If statements:

Now, To add multiple if statements to the lambda function we cannot add it directly in one line like the previous example. If we add more than one if statement or if we add an elif statement it will throw an error.

Python3




df['Maths_spl Class'] = df["maths"].apply(
  lambda x: "No Need" if x>=5 elif x==5 "Hold" else "Need")


Output:

df['Maths_spl Class'] = df["maths"].apply(lambda x: "No Need" if x>=5 elif x==5 "Hold" else "Need")
                                                                             ^
SyntaxError: invalid syntax

To solve this we can add the if statements to a traditional function and call the function with the apply() method in the dataframe.

syntax: def conditions():

               …conditions

In the following program, we are classifying the students according to the maths marks. We need to classify the students for the maths special class. Now we are going to classify the students with more than 8 points as “No need”, and the students less than 5 marks as “Need” and we are going to leave the rest of the student’s decisions on hold.

Python3




# Import the library
import pandas as pd
 
# dataframe
df = pd.DataFrame({'Name': ['John', 'Jack', 'Shri',
                            'Krishna', 'Smith', 'Tessa'],
                   'Maths': [5, 3, 9, 10, 6, 3]})
 
# Defining all the conditions inside a function
def condition(x):
    if x>8:
        return "No need"
    elif x>=5 and x<=7:
        return "Hold decision"
    else:
        return 'Need'
 
# Applying the conditions
df['Maths_Spl Class'] = df['Maths'].apply(condition)
 
print(df)


Output:

     Name  Maths Maths_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

So as you can see we have successfully passed multiple if statements in the dataframe.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads