[ad_1]

## Data Science

## Learn from real and most practical examples

**Lambda — Use it Wisely!! **💡

Python is powerful language for data analysis not only because it is easy to use but it offers a huge bunch of built-in functions, methods to deal with data.

Lambda function is one of such useful functions in Python, which makes it extremely easy to define and use the function in just one line of code. So, it is good to know in and outs of it to use it seamlessly for your data analysis tasks.

Therefore, here I listed 3 practical use-cases of lambda function with examples. Also you will find why and when not to use lambda function. 🏆

Even though it is a quick read, you can directly navigate to your favorite section using the index below.

**· ****What is Lambda Function in Python?**

· **Filter a List using Lambda Function**

· **Use Lambda function in map()**

· **Use Lambda function in apply()**

· **When not to use a Lambda function**

∘ Assigning a lambda expression to a variable

∘ Passing function inside lambda function

∘ Use lambda function when one-liner is poor choice

Before starting with the use-cases let’s quickly see what is lambda function and how to use it.

It is anonymous function or the function without name. Unlike other user-defined functions in Python, lambda function can be defined and called in the same line of the code.

This feature essentially makes it very handy to use in the situations where you want to do repetitive tasks on multiple items but in minimal code.

Every lambda function starts with the keyword

followed by function parameters and function body. Function parameters and the body is separated by colon i.e. **lambda**

.**:**

For example, in the function `lambda x: x**2`

,

is the function parameter and **x**

is the expression i.e. function body. You can apply this function on single variable like below,**x**2**

(lambda x:x**2)(5)# Output25

So the operation mentioned in the expression (*in this case **x**2** is square of *

) is performed on the number *x**5*.

📌 Note: You can include as many parameter you want in the lambda function but the function body can have only one expression. And this makes it different from other user-defined functions.

Now, let’s see the most practical use-cases for this amazing function.

One of the simplest use-case of lambda function is to filter a given list based on certain criteria. For this you can use lambda function with another built-in function

.**filter()**

In Python, `filter()`

is applied on an iterable such as a list or a dictionary to create a new iterable which essentially contains only those items of original iterable that satisfy certain condition. And this condition is defined with a function.

As the syntax for filter() is

, logically the lambda function can be used to define the condition which can be used in the filter().**filter(function, iterable)**

For example, you have a list with numbers from 2 to 50 at the interval of 3. And you want to get only those numbers whose square is less than 100. So the condition can be defined as lambda function as below —

**lambda x:x**2<100**

And it can be used in filter like this,

import numpy as np

yourlist = list(np.arange(2,50,3))list(filter(lambda x:x**2<100, yourlist))# Output[2, 5, 8]

If you look at the intermediate step

, it returns a **filter(lambda x:x**2<100, yourlist)***filter object*, which indeed needs to be converted into data structure such as list or set, which is done in the last line of code above.

Certainly, for complex calculations in the function, you should go for a normal user-defined function, but when you have a function with just one expression, lambda function is perfect choice. 💯

Next, rather than filtering the list, it can also be interesting to know how to apply a function on each item of an iterable.

In Python,

is the function which literally maps or operates passed function on each item in the iterable.**map()**

It has the same syntax as previous function,

. So, just like you did in filter(), you can pass the lambda function in map().**map(function, iterable)**

For example, suppose you want to get the addition of square and cube of each item in the list. so the lambda function will be,

**lambda x: x**2+x**3**

And similar to filter(), map() function returns map object which needs to be converted into other data structure. So the required result can be obtained as,

list(map(lambda x: x**2+x**3, yourlist))# Output

[12,

150,

576,

1452,

2940,

5202,

8400,

12696,

18252,

25230,

33792,

44100,

56316,

70602,

87120,

106032]

But, this was quite simple example, right??

In another example, suppose you have two lists as `mylist`

and `yourlist`

of same length like this —

`mylist = list(np.arange(4,52,3))`

yourlist = list(np.arange(2,50,3))

And you want to get the addition of square of elements at the same position in both lists. This can be super-easy using `map()`

function as below

`list(map(`**lambda x,y: x**2+y**2**, **yourlist,mylist**))

Please note that, as you defined two variables in the lambda function as x and y, you need to provide two iterables in the map function i.e. yourlist and mylist.

The sequence of these iterables in the map() or even in filter() matters as the first iterable is always passed as x whereas second iterable is passed as y. 💯

Moving ahead, you can also use the lambda function to transform a pandas series object.

A pandas series is actually the one dimensional array of indexed data. Unlike list, series always contains data of same data type. And that’s makes the base of Pandas DataFrame where each column of DataFrame is a pandas series.

To transform pandas series, you can use the function apply(). As its name suggests, it is used to apply a function on a pandas series.

Let’s create a pandas Series to get hold of the concept using an example.

`myseries = pd.Series(mylist)`

myseries

The syntax of apply() is slightly different than previous two functions but the logic is same — the function passed in apply() is applied on each element of the series. 💯

For an instance, suppose you want to add 5 in each element of the *myseries* and then divide it by square of the same element. So the lambda function will be `lambda x: (x+5)/x**2`

and it can be used in apply as below,

**myseries.apply(lambda x: (x+5)/x**2)**

Unlike previous two functions, you don’t need to pass the iterable or series to apply() but you can directly use it on the series using `.apply(function)`

.

Rather than applying on a single series, it makes more sense to use it in DataFrame. For example, you have a *dummy sales data* (can be downloaded for free under MIT license from **Github**) in DataFrame `df`

.

`df = pd.read_csv("Dummy_Sales_Data_v1.csv")`

df.head()

And you want to make the name of every sales Manager uppercase. This can be easily done by using lambda function in **.apply()** method.

`df["Sales_Manager"] = df["Sales_Manager"]`**.apply(lambda x: x.upper())**

df.head()

You can simply use `.apply()`

on any column, define a quick transformation using lambda function and assign the results back to the original column. 💯

.apply() with lambda function is 1.4X faster than str.upper()

When you compare `.apply()`

with lambda function with string methods to do the same transformation, you can see that former is faster than later. And if you compared it with for loop, then `.apply()`

is ** 300X** faster.

I personally find

with lambda function quite useful in most of my data analytics tasks.**.apply()**

As you learned about how and when you can use the lambda function in Python, it is also important to know when not to use it. 🎯

Certainly there are few use-cases of Lambda function which are not recommended by Python standard coding guidelines.

It is not wrong to use lambda function in below 3 cases, but simply it is not a best practice and hence should be avoided.

## Assigning a lambda expression to a variable

When you need a simple function, you might think of using lambda function to define it and use it when needed. However, this is not best practice.

By definition, lambda is an anonymous function, hence it is not a good idea to store it in a variable for future use.

For example, suppose you want to get sum of squares of two numbers and you want to do this transformation multiple times in your project then,

Bad practice ❌

squared_sum = lambda x,y: x**2 + y**2

squared_sum(3,4)#Output

25

Best practice ✅

def squared_sum(x,y):

return x**2 + y**2#Output

25

User defined functions are commonly used when you want to use same block of code multiple times. And hence, it should be preferred over lambda functions in such situations.

Moreover, I also observed many data professionals passing a standard function such as `sqrt`

in the lambda function, which is not a good practice.

## Passing function inside lambda function

This scenario arises when you want to use a function in map() or filter() or apply() method. This includes all built-in functions and user defined functions which takes exactly one argument.

For example, suppose you have a list of numbers and want to get square root of these numbers. This can be obtained using the built-in function **sqrt **available in **math** library.

Bad practice ❌

import math

mylist = [10, 25, 40, 49, 65, 81]

sqrt_list = list(map(lambda x: math.sqrt(x), mylist))

sqrt_list#output

[3.1622776601683795, 5.0, 6.324555320336759, 7.0, 8.06225774829855, 9.0]

Best practice ✅

import math

mylist = [10, 25, 40, 49, 65, 81]

sqrt_list = list(map(math.sqrt, mylist))

sqrt_list#output

[3.1622776601683795, 5.0, 6.324555320336759, 7.0, 8.06225774829855, 9.0]

The built-in function **sqrt** from **math** library can be directly used within map() function. Using it inside the lambda function is unnecessary and can impact the performance.

If you compare above two practices of passing another function within lambda function and run 100000 iterations for each, you can see that best practice always improves the performance. 💯

Passing function directly to map() is 1.8X faster!!

The same is true for user-defined functions.

Going ahead, one-liners in Python are quite popular and coders usually fall for it. However, one-liners are not always the best choice and using lambda function in such situations even worsens the code readability and makes it difficult to maintain.

## Use lambda function when one-liner is poor choice

When you use a complex operation within lambda function such as if-else conditions or simply multiple operations, the actual lambda function definition becomes longer.

And when you use such lambda function in one-liner, your one-liner code no longer fits into one line.

As per PEP8 guidelines, Limit all lines to a maximum of 79 characters.

As the code is written on the multiple lines, it becomes difficult to read the code efficiently and debug it if needed.

For example, you have a *dummy sales data* (can be downloaded for free under MIT license from Github) in DataFrame df.

`df = pd.read_csv("Dummy_Sales_Data_v1.csv")`

df.head()

And you want to create a new column with *Final Status* such that if the *Status* column contains *‘Delivered’* or *‘Shipped’* the Final status will be *‘Completed’* otherwise it will be *‘Not Completed’*

A one liner and bad practice ❌

`df["FinalStatus"] = df["Status"].map(lambda x: 'Completed' if x == 'Delivered' or x == 'Shipped' else 'Not Completed')`

This one liner contains 118 characters and it is extended beyond one line. So it is difficult to read.

Although it gives you correct output, I recommend below best practice. ✅

df["FinalStatus"] = ''df.loc[(df["Status"] == 'Delivered') |

(df["Status"] == 'Shipped'),

'FinalStatus'] = 'Completed'df.loc[(df["Status"] == 'Not Delivered') |

(df["Status"] == 'Not Shipped'),

'FinalStatus'] = 'Not Completed'

In general, one liner is poor choice when you want to filter the dataset based on multiple conditions. Rather easily split the code into multiple lines to improve its readability.

I personally use this best practice frequently, as it gives you more control over the code and you can anytime add more conditions if needed. 🏆

That’s all you need to know about lambda function in Python!

[ad_2]

Source link