Data_Snapshot

Lambda Functions

Lambda functions are an easy way to quickly define functions within Python. They are a departure from the regular def format for creating functions and can be confusing at first. Let’s begin with defining a simple square root function using the normal def syntax, and then converting it into a lambda function.

import numpy as np

def root(x):
    return np.sqrt(x)

In this instance any time we call root it will return the square root of x.

root(16)
4.0

Now we are going to replicate root as lamroot, where lamroot is defined using lambda.

lamroot = lambda x: np.sqrt(x)

Once again calling lamroot will return the square root of x.

lamroot(16)

Lets piece apart what has gone on here.

Lambda lets us define an outcome against a parameter with some sort of function, in this case x is the parameter, and NumPy’s square root function is being called against x to create the outcome. The split between the parameter and the function is the colon, everything before are parameters, everything after is the manipulations the function is making.

We can define a lambda function with more than one parameter.

lamroot2 = lambda x,y: np.sqrt(x+y)

Here we have given the function two parameters, x and y, and it will return the square root of the sum of the two parameters.

lamroot2(10,6)
4.0

From the structure we have laid out so far you are able to replace many functions throughout your code with lambda functions. There is a drawback to lambda functions in they do sacrifice some clarity in exchange for brevity. It is up to you to determine which is more useful in your situation.

Lambda functions really come into there own when applied to collections of data, such as lists or pandas DataFrames. Instead of writing a loop to manipulate these sorts of collections we can instead write a map or apply statement against the collection to have every piece calculated at once.

To demonstrate this I’m going to import pandas and spin up a quick DataFrame with the integers from 0 to 10.

import pandas as pd

df = pd.DataFrame(np.arange(0,11))

df
0
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10

Now we rewrite the previous lamroot function within an apply function on the DataFrame.

df = df.apply(lambda x: np.sqrt(x))
df
0
0 0.000000
1 1.000000
2 1.189207
3 1.316074
4 1.414214
5 1.495349
6 1.565085
7 1.626577
8 1.681793
9 1.732051
10 1.778279

Here we have applied the function against the entire DataFrame, viewing it now will show our integers have been turned into their square roots. This is very useful to write quick single line functions to cleanup or clarify a specific aspect of a dataset.