Lambda Functions & Alternative Methods in Python
Lambda Functions & Alternative Methods in Python
At the end of each method, I checked whether the output from lambda
function and the alternative method in python are identical(y/n).
Happy scripting.
import pandas as pd
# initialization of list
students_record= [['A',900],['B',750],['C',895],
['D',800],['E',850],['F',950]]
print(df)
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)
print(df)
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)
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)
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)
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)
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