Data Science

Let’s Learn About “Lambda Functions”

How to apply Lambda functions in data frames

Ravi Chandra

--

Image by Author
Index Of Contents
· Introduction
· What is Lambda Function
· Applied to Single Column
· Multiple Columns
· Single Row Using
· Filtering Data by Applying Lambda Function
· Use of map() function
· Bonus: if-else Statement
· Conclusion

Introduction

The lambda function solves many data science problems in Pandas. It provides the options to on both rows and columns to dataframes.

We will explore in this post the ways to apply the lambda functions to pandas data frames.

There are several use-cases of lambda function on pandas like filter(), map(), and conditional statements that we will explore with the help of some examples in this post.

What is Lambda Function

Lambda function contains a single expression.Often called as small anonymous function meaning it doesn’t require any name.

The lambda function is useful to solve small problems with less code. A function can take any number of arguments, but can only have one expression.

The following syntax is used to apply a lambda function on pandas dataframe:dataframe.apply(lambda x: x*2)

Applied to Single Column

We can use dataframe.assign() method which applies the Lambda function on a single column. Let’s look at an example .

In this example, we have applied a lambda function on the column Students Marks. After applying the Lambda function, the student percentages are calculated and stored in a new Percentage column.

Here is the data frame for which we are going to add new column Percentage which is the calculated percentage of the marks obtained by each student for max marks of 1000

Below is the implementation of apply in a lambda function on a single column Student Marks of the above dataframe.

We are successfully able to add new column as Percentage using assign method of Lambda on Single column

Multiple Columns

As we applied assign method on single column can also apply the function on multiple columns using the same assign()method in Pandas.

For this example, we have four columns Student Names, Computer, Math, and Physics. We will apply Lambda function on multiple subjects columns such as Computer, Math, and Physics to calculate the obtained marks stored in the new column as Marks_Obtained.

Here we will create new column as Marks_Obtained which is basically the simple sum of all the subjects i.e Student Names, Computer, Math, and Physics

The resulted data frame will have new column Marks_Obtained

Single Row Using

Until now we learnt how to use lambda function assign() method to manipulate columns. lambda provides many ways to use apply() on rows.

Here we will use apply() method which applies the Lambda function on a single row. To demonstrate this use-case let’s create sample data frame with Names,Age and Monthly Income columns.

To the above data frame, we will use lambda function on single row axis=1. Using this function, we will increment each person’s Monthly Income by 1000.

In the above example we used apply() to increment the value of row.

We can also filter the data using lambda function and then use apply() function on the selected rows. We will learn the same in next section

Filtering Data by Applying Lambda Function

As mentioned Lambda provides option of filtering the data which can further extended to use apply function to manipulate selected rows only

The filter() function takes pandas series and a lambda function. The Lambda function applies to the pandas series that returns the specific results after filtering the given series.

In the above example, we applied lambda function on the Age column and filtered the age of people under 25 years.

Use of map() function

And the last lambdafunction we will discuss today is the use of map() function.

The lambda function when applied on series to map the series based on the input correspondence. This function is useful to substitute or replace a series with other values.map() is a function which takes two arguments:

When we use the map() function, the size of input will be equal to output.To understand the concept of the map() function, see the following source code implementation.

We will create new data frame with Names,Age and Monthly Income columns.

As mentioned earlier map() function can be used to update the existing column. Here we will increase the value of Monthly Income column with 50%

The resulted data frame has Monthly Income column updated.

Bonus: if-else Statement

We can also apply the conditional statements like if or if-elseon pandas dataframes using the lambda function.

To demonstrate the same we will create the data frame withNames,Age and Monthly Income columns.

To the above data frame we will use conditional statement inside the lambda function Monthly Income column. Here we will create new column as Category which will be Graded if the monthly income is greater and equal to 5000 otherwise UnGraded.

The resulted data frame has new column as Category with Graded or UnGraded based on Monthly Income value of each row

Conclusion

In this post, we implemented various ways of applying the Lambda function on Pandas data frame. We saw how to apply the lambda function on rows and columns using the dataframe.assign() and dataframe.apply() methods respectively

We also demonstrated the different applications of the lambda function on pandas dataframe series, using functions like filter() , map() . Also in the end we touched upon the use of conditional statements.

Hope you learnt something new about Lambda function.

Until next time…Happy coding !!

--

--

Ravi Chandra

Full Stack Developer | Java | Python | Machine Learning | Data Science | Self-Development