Using Apply in Pandas Lambda functions with multiple if statements
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:
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.
syntax: df[ ‘Result’ ] = df[ ‘Maths’ ].apply( lambda x: ‘Pass’ if x>=5 else ‘Fail’ )
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.
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():
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.
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.