Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

LAMBDA FUNCTIONS & ALTERNATIVE METHODS IN PYTHON

Written: Manasa Ghatti


Date: Sep 14 2022

I found a great article on lambda functions


"https://blog.jovian.ai/lets-learn-about-lambda-functions-
6d51a902e443".
The same functionality, I am performing in regular data processing
tasks with alternative methods in python.
Here I am presenting a comparison of lambda functions and
equivalent alternative methods available in python to process data.

At the end of each method, I checked whether the output from lambda
function and the alternative method in python are identical(y/n).

Exploring all the functionalities of lambda functions and


alternative methods is beyond the scope of this article.

In this article I am exploring some of the alternative functions


available in python for data processing.
The focus is to provide the reader some of the alternative methods
available in python.
There might be other alternative methods available to accomplish
the functionality in data processing mentioned herein.

I did not run any comparative performance metrics on any of these


methods.

Happy scripting.

import pandas as pd
# initialization of list
students_record= [['A',900],['B',750],['C',895],
['D',800],['E',850],['F',950]]

#Applied on one column


print('#####################################\n','Applied on one
column')
# pandas dataframe creation
df = pd.DataFrame(students_record,columns=['Name','Marks'])

print(df)

#Below is the implementation of apply in a lambda function on a


single column
# using Lambda function

1
df_lambda = df.assign(Percentage = lambda x: (x['Marks'] /1000 *
100))
print(df_lambda)

##alternative construct
df_alternative = df.copy()
df_alternative['Percentage'] = df_alternative['Marks'] /1000 * 100
print(df_alternative)

#Checking lambda function output and alternative method output are


identical
print(df_lambda.equals(df_alternative))
#####################################
#Applied on n columns
print('#####################################\n','Applied on n
columns')
# nested list initialization
values_list = [['A',85, 75, 100], ['B', 90, 75, 90], ['C', 95, 82,
80],
['D', 75, 88, 68], ['E', 80, 63, 70], ['F', 91, 64,
90]]

# pandas dataframe creation


df = pd.DataFrame(values_list, columns=['Name','Computer', 'Math',
'Physics'])

print(df)

df_lambda = df.assign(Marks_Obtained=lambda x: (x['Computer'] +


x['Math'] + x['Physics']))
print(df_lambda)
##alternative construct
df_alternative = df.copy()
df_alternative['Marks_Obtained'] = df_alternative['Computer'] +
df_alternative['Math'] + df_alternative['Physics']
print(df_alternative)

#Checking lambda function output and alternative method output are


identical
print(df_lambda.equals(df_alternative))
#####################################
#Using a single row
print('#####################################\n','Using a single
row')

2
df=pd.DataFrame({
'ID':[1,2,3,4,5],
'Name':['A','B','C','D','E'],
'Age':[20,25,15,10,30],
'Income':[4000,6000,5000,2000,8000]
})

print(df)

df_lambda = df.copy()
df_lambda['Income']=df_lambda.apply(lambda x:
x['Income']+1000,axis=1)
print(df_lambda)

##alternative construct
df_alternative = df.copy()
df_alternative['Income']=df_alternative['Income']+1000
print(df_alternative)

#Checking lambda function output and alternative method output are


identical
print(df_lambda.equals(df_alternative))
#####################################
#Filtering data
print('#####################################\n','Filtering data')
df=pd.DataFrame({
'ID':[1,2,3,4,5],
'Name':['A','B','C','D','E'],
'Age':[20,25,15,10,30],
'Income':[4000,6000,5000,2000,8000]
})

print(df)
lambda_list = list(filter(lambda x: x<25,df['Age']))
print(lambda_list)
##alternatve construct
alternative_list = df[df['Age']<25]['Age'].to_list()
print(alternative_list)

#Checking lambda function output and alternative method output are


identical
if lambda_list == alternative_list:
print('True ')
else:
print('False')

3
#####################################
#Using map() function
print('#####################################\n','Using map()
function')
df=pd.DataFrame({
'ID':[1,2,3,4,5],
'Name':['A','B','C','D','E'],
'Age':[20,25,15,10,30],
'Income':[4000,6000,5000,2000,8000]
})

print(df)

df_lambda = df.copy()
df_lambda['Income']=list(map(lambda x:
int(x+x*0.5),df_lambda['Income']))
print(df_lambda)

##alternative construct
df_alternative = df.copy()
df_alternative['Income'] =
(df_alternative['Income']*1.5).astype("int64")#converting float
multiplication product to int
print(df_alternative)

#Checking lambda function output and alternative method output are


identical
print(df_lambda.equals(df_alternative))
######################################if-else conditional logic
print('#####################################\n','if-else
conditional logic')
df=pd.DataFrame({
'ID':[1,2,3,4,5],
'Name':['A','B','C','D','E'],
'Age':[20,25,15,10,30],
'Income':[4000,6000,5000,2000,8000]
})

print(df)

df_lambda = df.copy()
df_lambda['Category']=df_lambda['Income'].apply(lambda x: 'Graded'
if x>=5000 else 'UnGraded')
print(df_lambda)

4
##alternative construct
import numpy as np
df_alternative1 = df.copy()

df_alternative1['Category']=np.where(df_alternative1['Income']>=500
0, 'Graded','UnGraded')
print(df_alternative1)

df_alternative2 = df.copy()
df_alternative2['Category']=['Graded' if x>=5000 else 'UnGraded'
for x in df_alternative2['Income']]
print(df_alternative2)

#Checking lambda function output and alternative method output are


identical
print(df_lambda.equals(df_alternative1))
print(df_lambda.equals(df_alternative2))
######################################

OUTPUT FROM THE SCRIPT'S EXECUTION


#####################################
Applied on one column
Name Marks
0 A 900
1 B 750
2 C 895
3 D 800
4 E 850
5 F 950
Name Marks Percentage
0 A 900 90.0
1 B 750 75.0
2 C 895 89.5
3 D 800 80.0
4 E 850 85.0
5 F 950 95.0
Name Marks Percentage
0 A 900 90.0
1 B 750 75.0
2 C 895 89.5
3 D 800 80.0
4 E 850 85.0
5 F 950 95.0
True
#####################################
Applied on n columns
Name Computer Math Physics

5
0 A 85 75 100
1 B 90 75 90
2 C 95 82 80
3 D 75 88 68
4 E 80 63 70
5 F 91 64 90
Name Computer Math Physics Marks_Obtained
0 A 85 75 100 260
1 B 90 75 90 255
2 C 95 82 80 257
3 D 75 88 68 231
4 E 80 63 70 213
5 F 91 64 90 245
Name Computer Math Physics Marks_Obtained
0 A 85 75 100 260
1 B 90 75 90 255
2 C 95 82 80 257
3 D 75 88 68 231
4 E 80 63 70 213
5 F 91 64 90 245
True
#####################################
Using a single row
ID Name Age Income
0 1 A 20 4000
1 2 B 25 6000
2 3 C 15 5000
3 4 D 10 2000
4 5 E 30 8000
ID Name Age Income
0 1 A 20 5000
1 2 B 25 7000
2 3 C 15 6000
3 4 D 10 3000
4 5 E 30 9000
ID Name Age Income
0 1 A 20 5000
1 2 B 25 7000
2 3 C 15 6000
3 4 D 10 3000
4 5 E 30 9000
True
#####################################
Filtering data
ID Name Age Income
0 1 A 20 4000
1 2 B 25 6000

6
2 3 C 15 5000
3 4 D 10 2000
4 5 E 30 8000
[20, 15, 10]
[20, 15, 10]
True
#####################################
Using map() function
ID Name Age Income
0 1 A 20 4000
1 2 B 25 6000
2 3 C 15 5000
3 4 D 10 2000
4 5 E 30 8000
ID Name Age Income
0 1 A 20 6000
1 2 B 25 9000
2 3 C 15 7500
3 4 D 10 3000
4 5 E 30 12000
ID Name Age Income
0 1 A 20 6000
1 2 B 25 9000
2 3 C 15 7500
3 4 D 10 3000
4 5 E 30 12000
True
#####################################
if-else conditional logic
ID Name Age Income
0 1 A 20 4000
1 2 B 25 6000
2 3 C 15 5000
3 4 D 10 2000
4 5 E 30 8000
ID Name Age Income Category
0 1 A 20 4000 UnGraded
1 2 B 25 6000 Graded
2 3 C 15 5000 Graded
3 4 D 10 2000 UnGraded
4 5 E 30 8000 Graded
ID Name Age Income Category
0 1 A 20 4000 UnGraded
1 2 B 25 6000 Graded
2 3 C 15 5000 Graded
3 4 D 10 2000 UnGraded
4 5 E 30 8000 Graded

7
ID Name Age Income Category
0 1 A 20 4000 UnGraded
1 2 B 25 6000 Graded
2 3 C 15 5000 Graded
3 4 D 10 2000 UnGraded
4 5 E 30 8000 Graded
True
True

Reference:
https://blog.jovian.ai/lets-learn-about-lambda-functions-
6d51a902e443

You might also like