November 23, 2024

Lambda Functions in Python and Application to Map Function, Filter Function, and Pandas DataFrames

In this post, we introduce lambda functions and explain how these functions can be used together with

  1. Map function
  2. Filter function
  3. Pandas DataFrame structures

The YouTube video accompanying this post is given here

In Python, lambda functions are anonymous functions that are usually defined without explicit names (we can also name them). They can take many arguments, however, they can only return a single expression. They are often used with other functions, such as map, filter, and in Pandas DataFrame structures.

Let us consider an example. We want to write a function that increases the value of a variable for 1. This function has the following form

def incrementValue(x):
    return x+1

By calling, for example incrementValue(2), we obtain 3 as the output.

We can achieve this, by defining a lambda function as follows, and by calling it in the same line with an argument

(lambda x: x+1)(2)

Here “lambda x: x+1” is the lambda function. x is the argument, and x+1 is the expression that this function returns. We can also name the lambda function and call it on another code line

doubleValue = lambda x: 2*x
doubleValue(4)

As mentioned previously, lambda functions can take several arguments. Consider the following examples

(lambda x,y: x+2*y)(2,2)
linearLambda=lambda x,y: x+2*y
linearLambda(2,7)

We can also incorporate conditional statements in lambda functions

doubleTriple= lambda x,y: 2*x if (y=='Double') else (3*x if y=='Triple' else x)
doubleTriple(2,'Double')

Here, the value 2 will either be doubled or tripled, on the basis of the second lambda function argument.

Next, we explain how to use lambda functions with filter() function.

# use of the lambda functions with filter() Function
list1=[10,-1, 5, 8, 20, 50]
filtered=filter(lambda x: x<9, list1)
filteredList=list(filtered)

Here, we filter list1 on the basis of the condition x<9. That is, the values of list1 that are smaller than 9 will be included in the result that is given by “filteredList”.

Next, we explain how to use the lambda functions with map() function.

# use the map function
map1=map(lambda x: 2*x, list1)
map1list=list(map1)

Here, we create another list, from the original list1 by doubling its entries.

Finally, we can use lambda functions with Pandas DataFrame objects:

# use lambda functions with pandas
dictionary1={'data_1': [2,3,4,5,6, -1, -7, -2], 'data_2':[5,8,10,13,11,-2,-4,-8]}
dataFrame1=pd.DataFrame(dictionary1)
# define a new columns that is 3 times the second column 
dataFrame1['data_3']=dataFrame1['data_2'].map(lambda x: 2*x)

Here we define dataFrame1 from dictionary1. Then by using the lambda function we define a new column, called ‘data_3’ whose entries are computed by doubling the entries of the column ‘data_2’.

Here is another useful example

# this is very useful
dataFrame1['data_5']=dataFrame1['data_2'].map(lambda x: 2*x if x>0 else 0)

Here we define a new column called ‘data_5’, by doubling positive entries of ‘data_2’, and assigning 0 to entries of ‘data_5’ if the entries of ‘data_2’ are negative.

Here is another example of using lambda functions. Here we use lambda function with apply(). The code below is self-explanatory.

# another approach by using .apply() function
dataFrame1['data_4']=dataFrame1['data_2'].apply(lambda x: 3*x)