Lambda Functions in Python

Many times during data analysis, we need to create our own custom functions to perform the task. This can be done using Lambda functions in Python. Learning to create your own function in Python can be super helpful and it is very easy. It can be created in 2 ways:

  • Normal Functions
  • Lambda Functions

We use lambda functions when we require a nameless function. It is generally passed as an argument to some other python in-built functions.

Lambda Functions (Anonymous Function)

Syntax:   

 lambda arguments: expression
  • Lambda functions are used when we want to pass a function for a short period of time.
  • Lambda Functions are passed as an argument to other python functions.
  • They can have many arguments but evaluate and return only one expression.
  • There’s no need to use return statement in the lambda function.
#Normal Function - We define normal function with `def` keyword.
def power(num, power):          
    return num**power           
# There are 2 parameters/arguments- num, power
# Return gives the value that function will give on execution

power(3, 4)

81    #Output

The above function takes 2 arguments: num and power and return num ^ power.

Note: In python ** means power (^). 3**2 = 9

Let’s write the same function using lambda function.


# Lambda Function - We define lambda function with `lambda` keyword.
power_1 = lambda x,y: x**y     
# We gave the function 2 arguments: x & y

power_1(5, 3)

125 #Output

Map function in Python

Syntax:   

map(function, iterable...)

Map function in Python applies the function given to every element of iterable and returns the values. The function can either be a normal function or a lambda function.

num = [1, 2, 3, 4, 5, 6]                        # We created a list of numbers.
num_sq =  list(map(lambda x: x**2, num))        # The function given returns the square of the number.
num_sq

[1, 4, 9, 16, 25, 36]        #Output

Map took the elements one by one, squared them and assigned them to a list.

Note here that we didn’t assign any name to the function.

Filter function in Python

Syntax:    

filter(function, iterable...)

Filter function in Python applies the function to every element of iterable and returns only those elements which satisfy the condition given in the function.

num = [1, 2, 3, 4, 5, 6]
even_num = list(filter(lambda x: x%2 == 0, num))
even_num

[2, 4, 6]       #Output

% is modulo function in python. It gives the remainder. Eg, 5%2 = 1

The function we used in the above example says that return True if a number when divided by 2 gives remainder 0 i.e. if the number is even.

Lambda function on Titanic dataset

import pandas as pd               # Pandas library is used to import data in taubular format.
data = pd.read_csv("train.csv")
data.head(3)                      # Head function displays the top rows in the dataframe.
PassengerIdSurvivedPclassNameSexAgeSibSpParchTicketFareCabinEmbarked
0103Braund, Mr. Owen Harrismale22.010A/5 211717.2500NaNS
1211Cumings, Mrs. John Bradley (Florence Briggs Th…female38.010PC 1759971.2833C85C
2313Heikkinen, Miss. Lainafemale26.000STON/O2. 31012827.9250NaNS
Titanic Dataset
data.Age.isnull().sum()     
# isnull() tells which values are null/missing.
# summing them gives the count of missing values.

177      #Output

There are missing values in the Age column. We will use lambda function with apply to impute(fill) the missing values by the median based on Pclass and Sex of the passenger.

data.groupby(["Pclass", "Sex"]).Age.median()

#Output

Pclass  Sex   
1       female    35.0
        male      40.0
2       female    28.0
        male      30.0
3       female    21.5
        male      25.0
Name: Age, dtype: float64

This can be read as follows: The median age of female passengers in Pclass 1 is 35 years.

data.loc[:, "Age"] = data.groupby(["Pclass", "Sex"]).Age.apply(lambda x: x.fillna(x.median()))

#fillna() function fills the missing values

data.Age.isnull().sum()

0  #Output

This is one simple example showing how elegant and useful lambda functions are in data analysis.

Conclusion:

  • Lambda functions do not require a return statement to execute.
  • It requires less amount of code and gets the job done in literally one step.
  • They are most powerful when used with some other function.
  • Generally, they are used in data preprocessing to perform complex tasks.

This article is written by Madhav Samariya . I thank him to write this article and give us permission to post it here. I urge you to visit his Linkedin Profile and share your views or feedback with him.

Thank you for reading.

In case you have not tried earlier exercises and want to learn it from the start, we have the table below.

Python ArticleLinks
1. Introduction to PythonClick here
2. Conditional Execution using If, Ifelse and Elif in PythonClick here
3. Data Types and Variable Naming in PythonClick here
4. Introduction to Functions in PythonClick here
Python articles from The Actuarial Club

This article has been published here by Jinal Shah.

About the Author

Madhav Samariya

I'm a second year Statistics Major from Kirori Mal College, University of Delhi. I've cleared 2 Actuarial papers: CS 1 and CB 2. Being an Actuarial aspirant, the field of Data Science appeals me as well. I'm proficient in Excel, R, Python, SPSS and MySQL.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.